將CPU使用情況匯入MySQL表

2021-09-11 02:10:17 字數 2785 閱讀 3386

(內容來自書本《mysql技術內幕innodb儲存引擎》)

如果需要監控cpu的使用情況,可以通重載入/proc/stat來完成。首先需要建立一張監控cpu的表cpu_stat:

create table if not exists cpu_stat

(id bigint auto_increment primary key,

value char(25) not null,

user bigint,

nice bigint,

system bigint,

idle bigint,

iowait bigint,

irq bigint,

softirq bigint,

steal bigint,

guest bigint,

other bigint,

time datetime

);接著可以通過load data infile命令來載入/proc/stat檔案,但需要對其中一些數值進行轉化,命令如下所示:

load data infile '/proc/stat' ignore into table cpu_stat fields terminated by ' '

(@value, @val1, @val2, @val3, @val4, @val5, @val6, @val7, @val8, @val9, @val10)

setvalue = @value,

user = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val1, 0), ifnull(@val2, 0))),

nice = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val2, 0), ifnull(@val3, 0))),

system = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val3, 0), ifnull(@val4, 0))),

idle = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val4, 0), ifnull(@val5, 0))),

iowait = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val5, 0), ifnull(@val6, 0))),

irq = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val6, 0), ifnull(@val7, 0))),

softirq = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val7, 0), ifnull(@val8, 0))),

steal = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val8, 0), ifnull(@val9, 0))),

guest = if(@value not like 'cpu%', null, if(@value != 'cpu', ifnull(@val9, 0), ifnull(@val10, 0))),

other = if(@value not like 'cpu%', user + nice + system + idle + iowait + irq + softirq + steal + guest, @val1),

time = now();

接著可以設定乙個定時器來讓mysql資料庫自動地執行上述load data infile語句,這樣就會有每個時間點的cpu資訊被記錄到表cpu_stat。執行下述語句就可以得到每個時間點上cpu的使用情況。

select 

100 * ((new.user - old.user)/(new.other - old.other)) user,

100 * ((new.nice - old.nice)/(new.other - old.other)) nice,

100 * ((new.system - old.system)/(new.other - old.other)) system,

100 * ((new.idle - old.idle)/(new.other - old.other)) idle,

100 * ((new.iowait - old.iowait)/(new.other - old.other)) iowait,

100 * ((new.irq - old.irq)/(new.other - old.other)) irq,

100 * ((new.softirq - old.softirq)/(new.other - old.other)) softirq,

100 * ((new.steal - old.steal)/(new.other - old.other)) steal,

100 * ((new.guest - old.guest)/(new.other - old.other)) guest,

new.time

from cpu_stat old, cpu_stat new

where new.id - 15 = old.id

and old.value = 'cpu'

and new.value = old.value;

同樣,還可以對/proc/diskstat檔案執行如上所示的操作,這樣就可以對磁碟進行監控操作了。

gperftools分析cpu使用情況

gperftools是google提供的一套工具,其中的乙個功能是cpu profiler,用於分析函式的執行時間,安裝見參考鏈結,下面是乙個簡單的使用例項 一 test profiler.cpp 1 include 2 include 3using namespace std 45void tes...

MySQL資源使用情況

1.檢視所有資料庫容量大小 select table schema as 資料庫 sum table rows as 記錄數 sum truncate data length 1024 1024,2 as 資料容量 mb sum truncate index length 1024 1024,2 a...

Linux檢視記憶體 CPU使用情況

由於linux無圖形化顯示介面檢視所在伺服器的cpu 記憶體使用情況 需要通過如下指令操作 一 顯示當前使用情況 進入指令操作介面後輸入 top 即可呈現下列介面,即時顯示當前伺服器的cpu 記憶體使用情況 在這裡插入 二 顯示記憶體使用情況 輸入 free 可以轉換為gb為單位,相關命令為 fre...