Shell脚本实现网页监控动态 uptime 数据

Shell脚本实现网页监控动态 uptime 数据

uptime命令

  • 用于打印系统的运行时间等信息
# uptime 
 22:24:34 up  2:14,  2 users,  load average: 0.00, 0.01, 0.05
  • “22:24:34“表示当前时间
  • “up 2:14“ 表示系统已经连续运行2小时14分
  • “load average: 0.00, 0.01, 0.05“ 中显示的分别是系统过去1分钟、5分钟和15分钟的平均负载

1、测试命令

[root@test ~]# uptime 
 22:24:34 up  2:14,  2 users,  load average: 0.00, 0.01, 0.05
[root@test ~]# uptime | awk '{print $1,$(NF-2),$(NF-1),$NF}'
22:24:38 0.00, 0.01, 0.05
[root@test ~]# uptime | awk '{print $1,$(NF-2),$(NF-1),$NF}' | sed 's/,//g'
22:24:41 0.00 0.01 0.05
[root@test ~]#

2、编辑脚本

  • 命令写入脚本,并将生成的数据导入追加到指定文件
#脚本内容
[root@test ~]# cat /tmp/shell/uptime 
uptime | awk '{print $1,$(NF-2),$(NF-1),$NF}' | sed s/,//g  >> /tmp/shell/data

gnuplot <<EOF
set terminal png
set output '/var/www/html/uptime.png'
set xdata time
set timefmt '%H:%M:%S'
set xlabel 'TIME'
set format x '%H:%M'
set xtics rotate by -45
set ylabel 'load average'
plot '/tmp/shell/data' u 1:2 t '1-min' with lines,'/tmp/shell/data' u 1:3 t '5-min' with lines,'/tmp/shell/data' u 1:4 t '15-min' with lines
EOF
  • 赋予可执行权限
[root@test ~]# chmod +x /tmp/shell/uptime
  • 添加到任务计划(每分每时每日每月每周)
[root@test ~]# crontab -e
* * * * *   /tmp/shell/uptime
  • 查看数据情况(系统过去1分钟、5分钟和15分钟的平均负载)
[root@test ~]# cat /tmp/shell/data 
22:52:02 0.05 0.04 0.05
[root@test ~]# cat /tmp/shell/data 
22:52:02 0.05 0.04 0.05
22:53:01 0.02 0.04 0.05
[root@test ~]# cat /tmp/shell/data 
22:52:02 0.05 0.04 0.05
22:53:01 0.02 0.04 0.05
22:54:01 0.01 0.03 0.05

3、数据加压测试

#数据压力测试
[root@test ~]# dd if=/dev/zero of=/dev/null &
[1] 2182
#查看数据情况(系统过去1分钟、5分钟和15分钟的平均负载)
[root@test ~]# cat /tmp/shell/data 
22:52:02 0.05 0.04 0.05
22:53:01 0.02 0.04 0.05
22:54:01 0.01 0.03 0.05
22:55:01 0.30 0.09 0.07

#再加压处理两次
[root@test ~]# dd if=/dev/zero of=/dev/null &
[3] 2267
[root@test ~]# dd if=/dev/zero of=/dev/null &
[4] 2269
#查看数据情况(系统过去1分钟、5分钟和15分钟的平均负载)
[root@test ~]# cat /tmp/shell/data 
22:52:02 0.05 0.04 0.05
22:53:01 0.02 0.04 0.05
22:54:01 0.01 0.03 0.05
22:55:01 0.30 0.09 0.07
22:56:01 0.74 0.26 0.12
22:57:01 1.00 0.41 0.18
22:58:01 2.70 1.00 0.40

#查找 dd 进程
[root@test ~]# ps xa      #找到 dd 进程 ID
#直接 kill 掉
[root@test ~]# kill -9 2182
[root@test ~]# kill -9 2267
[root@test ~]# kill -9 2269

#查看数据(系统过去1分钟、5分钟和15分钟的平均负载)峰值下降
[root@test ~]# cat /tmp/shell/data 
22:52:02 0.05 0.04 0.05
22:53:01 0.02 0.04 0.05
22:54:01 0.01 0.03 0.05
22:55:01 0.30 0.09 0.07
22:56:01 0.74 0.26 0.12
22:57:01 1.00 0.41 0.18
22:58:01 2.70 1.00 0.40
22:59:01 2.89 1.36 0.56
23:00:01 2.60 1.60 0.70
23:01:01 0.96 1.31 0.65
23:02:01 0.43 1.08 0.62

3、安装处理工具

  • 安装软件

[root@test ~]# yum install -y gnuplot httpd

  • 开启Apache服务
[root@test ~]# systemctl start httpd
  • 查看图片是否生成
[root@test ~]# ls /var/www/html/
uptime.png
  • 浏览器访问

4、网页优化

  • 优化网页访问效果
  • 代码如下
[root@test ~]# cat /var/www/html/gnuplot.html
<html>
<head><meta http-equiv="refresh" content="5"></head>

<body>

<h1> performance charts</h1>

<img src="/uptime.png">

</body>

</html>
[root@test ~]#

猜你喜欢

转载自blog.csdn.net/sunny_future/article/details/80306513