提取sar值并导入数据库的shell脚本

发布时间:2020-06-17编辑:脚本学堂
分享一段shell脚本,用于提取sar的值,并导入数据库中,有需要的朋友参考下。

提取sar值并导入数据库中。

代码1:
 

复制代码 代码示例:
[root@jbxue sarlog]# cat /monitor/nagios/shell/sarlog/scan_sarlog.sh
#!/bin/sh
PDIR=/monitor/nagios/shell/sarlog
runday=$(date +%Y-%m-%d)
TmpDir=/monitor/nagios/tmp
for i in `/bin/cat /monitor/nagios/shell/sarlog/machine.lst`
do
   /usr/bin/scp ${PDIR}/sarlog.sh ${i}:/root && /usr/bin/rsh ${i} /root/sarlog.sh &
done

代码2:
 

复制代码 代码示例:

[root@jbxue sarlog]# cat sarlog.sh
#!/bin/sh
host=$(hostname)
channel=$(hostname | sed 's/[0-9]//g')
runday=$(date +%Y-%m-%d)
IPhost=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | linuxjishu/13830.html target=_blank class=infotextkey>awk '{ print $1}')

/usr/bin/sar -u 60 120 >  /tmp/$(hostname)-${runday}.txt
/bin/sed '/ +/s//,/g'  /tmp/$(hostname)-${runday}.txt |  /bin/sed -n '4,$p'  | grep -v Average  |awk -F "," '{print "'"$host"'"",""'"$channel"'"",""'"$IPhost"'"",""'"$runday"'"" "$1","$4","$6","$7","$9}'  > /tmp/$(hostname)${runday}.txt
rm -rf /tmp/$(hostname)-${runday}.txt

代码3:
 

复制代码 代码示例:
[root@jbxue sarlog]# cat /monitor/nagios/shell/sarlog/process_sarlog.sh
#!/bin/sh
PDIR=/monitor/nagios/shell/sarlog
runday=$(date +%Y-%m-%d)
TmpDir=/monitor/nagios/shell/sarlog/log
Machinelst=/monitor/nagios/shell/sarlog/machine.lst
for i in `/bin/cat /monitor/nagios/shell/sarlog/machine.lst`
do
   /usr/bin/scp ${i}:/tmp/*${runday}.txt ${TmpDir}
done
/bin/cat ${TmpDir}/*${runday}.txt >> ${TmpDir}/Tmp${runday}.txt
count=1
Line=$(cat ${TmpDir}/Tmp${runday}.txt |wc -l)
>${TmpDir}/Result${runday}.txt
while [ "$count" -le "$Line" ]
do
   Idle=$(sed -n "${count}p" ${TmpDir}/Tmp${runday}.txt | awk -F "," '{print $8}')     
   Busy=`echo "scale=2;100-$Idle"|bc`        
   sed -n "${count}p" ${TmpDir}/Tmp${runday}.txt | sed  "s/$/,$Busy/g" >> ${TmpDir}/${runday}Result.txt
   let count++
done
rm -rf ${TmpDir}/*${runday}.txt
/usr/local/mysql/bin/mysql -u root -D nagios -pbj7n48dM  <<EOF
   load data infile '${TmpDir}/${runday}Result.txt' into table t_daily_stats FIELDS TERMINATED BY  ',';
EOF

以上shell脚本实现了提取sar的值,并写入数据库,希望对大家有所帮助。