1. 程式人生 > >shell腳本抓取用戶存儲quota寫道mysql並展現到grafana面板

shell腳本抓取用戶存儲quota寫道mysql並展現到grafana面板

use 通過 ssh csv ota icinga2 exce mysql數據庫 home

通過shell腳本抓取存儲home用戶的空間使用情況,寫到excel文件裏,再導入到mysql數據庫,最後通過grafana進行展示

vi aa.sh

#!/bin/bash

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

Dir=/logs/Homes

/bin/rm -rf $Dir/quota2.txt
/bin/touch $Dir/quota2.txt

/usr/bin/ssh 10.0.0.10 "quota report -x" | grep home > $Dir/quota.txt
/usr/bin/ssh 10.0.0.20 "quota report -x" | grep home2 >> $Dir/quota.txt

/bin/cat $Dir/quota.txt | awk -F[:" "]+ ‘{print $2}‘ | tr -d "*" | grep -v -w "root" > $Dir/users.txt

for i in cat $Dir/users.txt
do
Used=cat $Dir/quota.txt | grep -w $i | awk -F[:" "]+ ‘{print $5}‘
Home=cat $Dir/quota.txt | grep -w $i | awk -F[:" "]+ ‘{print $3}‘
Filen=cat $Dir/quota.txt | grep -w $i | awk -F[:" "]+ ‘{print $8}‘


/bin/echo "$Date,$i,$Home,$Used,$Filen" >> $Dir/quota2.txt
done

/bin/cat $Dir/quota2.txt | grep $Date2 > $Dir/quota3.txt

/bin/cat $Dir/quota3.txt | tr -s ‘[:blank:]‘ > $Dir/quota4.csv

/usr/bin/mysql -h 10.0.0.2 -u icinga2 -p‘xxxx‘ --local-infile=1 ming -e "LOAD DATA LOCAL INFILE ‘/logs/Homes/quota4.csv‘ INTO TABLE userquota FIELDS TERMINATED BY ‘,‘"

:wq

mysql -u root -p‘xxxx‘

create database ming default character set utf8;
create table userquota(time datetime,user char(50),home char(50),used float,filen float);
grant all on ming.* to aa@‘localhost‘ identified by ‘xxxx‘;
exit

grafana配mysql略

查詢單個人空間使用情況語句:

SELECT UNIX_TIMESTAMP(time) as time_sec, used as ‘zhi.yang‘ FROM userquota WHERE user=‘zhi.yang‘;

查詢平均空間使用情況語句:

select Unix_timestamp(time) as time_sec, avg(used) from userquota group by time_sec;

shell腳本抓取用戶存儲quota寫道mysql並展現到grafana面板