通过shell抓取mysql应用的CPU、内存负载

注: 此处抓取的CPU使用率同top命令显示,即100%为1个core,是有可能超过100%的

vi dbload.sh

#!/bin/bash

Date=date +"%Y-%m-%d %H:%M:%S"

Datee=date +%Y-%m-%d

Se=/bin/hostname

Cp=/bin/ps aux | grep 'mysql.sock' | head -n 1 | awk -F[:" "]+ '{print $3}' Me=/bin/ps aux | grep 'mysql.sock' | head -n 1 | awk -F[:" "]+ '{print $4}'

/bin/echo "$Date,$Se,$Cp,$Me" > /log/dbload.txt

/bin/cat /log/dbload.txt | tr -s '[:blank:]' > /log/dbload2.csv

/usr/bin/mysql -h 10.0.0.2 -u icinga2 -p'xxxxxxx' --local-infile=1 dbload -e "LOAD DATA LOCAL INFILE '/log/dbload2.csv' INTO TABLE loadavg FIELDS TERMINATED BY ','"

:wq

mysql部分:

create database dbload default character set utf8; create table loadvag(time datetime,server char(50),cpu float,mem float); grant all on dbload.* to icinga2@'10.%' identified by 'xxxxx'; flush privileges; exit

grafana语句:

select UNIX_TIMESTAMP(Time) as time, cpu as "db2" from loadavg where server="db2";