shell脚本抓取用户存储quota写道mysql并展现到grafana面板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本抓取用户存储quota写道mysql并展现到grafana面板相关的知识,希望对你有一定的参考价值。

通过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 [email protected]‘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面板的主要内容,如果未能解决你的问题,请参考以下文章

python抓取jenkins slave总数online数offline数写道mysql并展现

通过grafana面板展示各home存储的空间使用率及文件数使用率

让ChatGPT帮我写shell脚本, 结局很感人

MySQL 数据库迁移工作小记----连接抓取展示与异常连接

shell脚本学习join

sh 用于创建MySQL数据库和用户的Shell脚本