Linux服务器系统自动巡检脚本生成html报告

本文主要是介绍Linux服务器系统自动巡检脚本生成html报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、 前言

1、在上一篇文章里给大家介绍oracle自动巡检脚本生成html报告,这篇文章介绍linux服务巡检脚本生成html报告。
2、脚本依然是简单脚本语句的堆积,方便大家二次编辑使用。
3、项目已经上传到我的github上
项目地址:oswatch.git

二、注意事项与报告部分截图

一定注意阅读git上的README.md说明,避免部分信息无法正常显示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、README.md

1、使用root用户执行
2、使用说明
1)、脚本中 ifconfig、mpstat、iostat命令有依赖,需要安装相关依赖包

yum -y install bc sysstat net-tools

2)、执行完巡检之后,将在脚本所在的路径下生成html巡检结果报告,如下
192.168.31.102os_linux_summary.html
3)、巡检项信息如下(其他统计项可根据实际需要自行添加)

1)、系统基本信息

2)、cpu信息

3)、ip网络信息

4)、cpu使用率

5)、连接数信息

6)、系统限制信息

7)、内存使用信息

8)、磁盘使用信息

9)、网络流量情况/系统资源变化

10)、cpu消耗情况前10排行

11)、cpu消耗按内存情况前10排行

12)、磁盘io情况

13)、系统定时任务列表

14)、系统定时任务脚本内容

四、脚本内容

#!/bin/bash
ipaddress=`ip a|grep "global"|awk '{print $2}' |awk -F/ '{print $1}'`
file_output=${ipaddress}'os_linux_summary.html'
td_str=''
th_str=''
#yum -y install bc sysstat net-tools
create_html_css(){echo -e "<html>
<head>
<style type="text/css">body        {font:12px Courier New,Helvetica,sansserif; color:black; background:White;}table,tr,td {font:12px Courier New,Helvetica,sansserif; color:Black; background:#FFFFCC; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;} th          {font:bold 12px Courier New,Helvetica,sansserif; color:White; background:#0033FF; padding:0px 0px 0px 0px;} h1          {font:bold 12pt Courier New,Helvetica,sansserif; color:Black; padding:0px 0px 0px 0px;} 
</style>
</head>
<body>"
}
create_html_head(){
echo -e "<h1>$1</h1>"
}
create_table_head1(){echo -e "<table width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
}
create_table_head2(){echo -e "<table width="100%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
}
create_td(){td_str=`echo $1 | awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'`
}
create_th(){th_str=`echo $1|awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<th>"$i"</th>";i++}}'`
}
create_tr1(){create_td "$1"echo -e "<tr>$td_str</tr>" >> $file_output
}
create_tr2(){create_th "$1"echo -e "<tr>$th_str</tr>" >> $file_output
}
create_tr3(){echo -e "<tr><td><pre style=\"font-family:Courier New; word-wrap: break-word; white-space: pre-wrap; white-space: -moz-pre-wrap\" >`cat $1`</pre></td></tr>" >> $file_output
}
create_table_end(){echo -e "</table>"
}
create_html_end(){echo -e "</body></html>"
}
NAME_VAL_LEN=12
name_val () {printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2"
}
get_physics(){name_val "巡检时间" "`date`"name_val "主机名" "`uname -n`"name_val "系统版本" "`cat /etc/{oracle,redhat,SuSE,centos}-release 2>/dev/null|sort -ru|head -n1`"name_val "内核版本" "`uname -r`"name_val "架构" "CPU=`lscpu|grep Architecture|awk -F: '{print $2}'|sed 's/^[[:space:]]*//g'`;OS=`getconf LONG_BIT`-bit"
}
get_cpuinfo () {file="/proc/cpuinfo"virtual=`grep -c ^processor "${file}"`physical=`grep 'physical id' "${file}" | sort -u | wc -l`cores=`grep 'cpu cores' "${file}" | head -n 1 | cut -d: -f2`model=`grep "model name" "${file}"|sort -u|awk -F: '{print $2}'`speed=`grep -i "cpu MHz" "${file}"|sort -u|awk -F: '{print $2}'`cache=`grep -i "cache size" "${file}"|sort -u|awk -F: '{print $2}'`SysCPUIdle=`vmstat | sed -n '$ p' | awk '{print $15}'`[ "${physical}" = "0" ] && physical="${virtual}"[ -z "${cores}" ] && cores=0cores=$((${cores} * ${physical}));htt=""if [ ${cores} -gt 0 -a $cores -lt $virtual ]; then htt=yes; else htt=no; finame_val "线程" "physical = ${physical}, cores = ${cores}, virtual = ${virtual}, hyperthreading = ${htt}"name_val "cpu型号" "${physical}*${model}"name_val "速度" "${virtual}*${speed}"name_val "缓存" "${virtual}*${cache}"name_val "CPU空闲率(%)" "${SysCPUIdle}%"
}
get_netinfo(){echo "interface | status | ipadds     |      mtu    |  Speed     |     Duplex" >>/tmp/tmpnet_h1_`date +%y%m%d`.txtfor ipstr in `ifconfig -a|grep ": flags"|awk  '{print $1}'|sed 's/.$//'`doipadds=`ifconfig ${ipstr}|grep -w inet|awk '{print $2}'`mtu=`ifconfig ${ipstr}|grep mtu|awk '{print $NF}'`speed=`ethtool ${ipstr}|grep Speed|awk -F: '{print $2}'`duplex=`ethtool ${ipstr}|grep Duplex|awk -F: '{print $2}'`echo "${ipstr}"  "up" "${ipadds}" "${mtu}" "${speed}" "${duplex}"\|awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6}'  >>/tmp/tmpnet1_`date +%y%m%d`.txtdone
}
get_cpuuse(){echo "#######################################  cpu使用率  #######################################" >>/tmp/tmp_cpuuse_`date +%y%m%d`.txtmpstat -P ALL 10 6 >>/tmp/tmp_cpuuse_`date +%y%m%d`.txt
}
get_connections (){filemax=`cat /proc/sys/fs/file-max`name_val "Number of concurrent connections" "${filemax}"
}
get_ulimitinfo(){echo "#######################################  系统限制最大进程数  #######################################" >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txtulimit -a >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
}
get_meminfo(){echo "Locator   |Size     |Speed       |Form Factor  | Type      |    Type Detail" >>/tmp/tmpmem3_h1_`date +%y%m%d`.txtdmidecode| grep -v "Memory Device Mapped Address"|grep -A12 -w "Memory Device" \|egrep "Locator:|Size:|Speed:|Form Factor:|Type:|Type Detail:" \|awk -F: '/Size|Type|Form.Factor|Type.Detail|^[\t ]+Locator/{printf("|%s", $2)}/^[\t ]+Speed/{print "|" $2}' \|grep -v "No Module Installed" \|awk -F"|" '{print $4,"|", $2,"|", $7,"|", $3,"|", $5,"|", $6}' >>/tmp/tmpmem3_t1_`date +%y%m%d`.txtfree -glht >>/tmp/tmpmem2_`date +%y%m%d`.txtmemtotal=`vmstat -s | head -1 | awk '{print $1}'`avm=`vmstat -s| sed -n '3p' | awk '{print $1}'`name_val "Mem_used_rate(%)" "`echo "100*${avm}/${memtotal}" | bc`%" >>/tmp/tmpmem1_`date +%y%m%d`.txt}
get_diskinfo(){echo "Filesystem        |Type   |Size |  Used  | Avail | Use%  | Mounted on | Opts" >>/tmp/tmpdisk_h1_`date +%y%m%d`.txtdf -ThP|grep -v tmpfs|sed '1d'|sort >/tmp/tmpdf1_`date +%y%m%d`.txtmount -l|awk '{print $1,$6}'|grep ^/|sort >/tmp/tmpdf2_`date +%y%m%d`.txtjoin /tmp/tmpdf1_`date +%y%m%d`.txt /tmp/tmpdf2_`date +%y%m%d`.txt\|awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6,"|", $7,"|", $8}' >>/tmp/tmpdisk_t1_`date +%y%m%d`.txt lsblk >>/tmp/tmpdisk1_`date +%y%m%d`.txt for disk in `ls -l /sys/block|awk '{print $9}'|sed '/^$/d'|grep -v fd`doecho "${disk}" `cat /sys/block/${disk}/queue/scheduler`  >>/tmp/tmpdisk2_`date +%y%m%d`.txt donepvs >>/tmp/tmpdisk3_`date +%y%m%d`.txtecho "======================  =====  =====  =====  =====  =====  ==========  =======" >>/tmp/tmpdisk3_`date +%y%m%d`.txtvgs >>/tmp/tmpdisk3_`date +%y%m%d`.txtecho "======================  =====  =====  =====  =====  =====  ==========  =======" >>/tmp/tmpdisk3_`date +%y%m%d`.txtlvs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
}
get_topproc(){#os loadecho "#######################################  网络流量情况  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txtsar -n DEV 10 6 >>/tmp/tmpload_`date +%y%m%d`.txtecho "#######################################  系统资源变化  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txtvmstat -S M 10 6  >>/tmp/tmpload_`date +%y%m%d`.txt#top cpumpstat 1 5 >>/tmp/tmptopcpu_`date +%y%m%d`.txtecho "#######################################  消耗CPU前十排行  #######################################" >>/tmp/tmptopcpu_`date +%y%m%d`.txtps aux|head -1 >>/tmp/tmptopcpu_`date +%y%m%d`.txtps aux|grep -v PID|sort -rn -k +3|head  >>/tmp/tmptopcpu_`date +%y%m%d`.txt#top memecho "#######################################  消耗内存前十排行  #######################################" >>/tmp/tmptopmem_`date +%y%m%d`.txtps aux|head -1 >>/tmp/tmptopmem_`date +%y%m%d`.txtps aux|grep -v PID|sort -rn -k +4|head  >>/tmp/tmptopmem_`date +%y%m%d`.txtecho "TOP10 CPU Resource Process" >>/tmp/tmptopmem_`date +%y%m%d`.txttop -bn1 -o "%CPU"|sed  -n '1,17p' #top i/oecho "#######################################  磁盘io情况  #######################################" >>/tmp/tmptopio_`date +%y%m%d`.txtiostat -k -d 10 5  >>/tmp/tmptopio_`date +%y%m%d`.txt
}
get_crontablist(){crontab -l >>/tmp/tmp_crontab_`date +%y%m%d`.txtif [ -s /tmp/tmp_crontab_`date +%y%m%d`.txt ] ; then echo 'ths file is not empyt and file info'elseecho '#### 无定时任务 ####' >>/tmp/tmp_crontab_`date +%y%m%d`.txtfi
}
get_crontab_content(){crontab_content_log=/tmp/tmp_crontab_content_`date +%y%m%d`.txtcrontab -l|awk -F ' ' '{ print $NF}' >>$crontab_content_logcontrab_num=`crontab -l|awk -F ' ' '{ print $NF}'|wc -l`if [ $contrab_num -ne 0 ];thencount=1while [ $count -le $contrab_num ]doecho "#######################################  获取系统定时任务脚本 $count 内容开始  #######################################" >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txtcat `sed -n -e "${count}p" $crontab_content_log` >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txtecho -e "\n#######################################  获取系统定时任务脚本 $count 内容结束  #######################################\n" >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txtcount=$[${count}+1]doneelseecho '#### 无定时执行脚本 ####' >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txtfi
}
create_html(){rm -rf $file_outputtouch $file_outputcreate_html_css >> $file_outputcreate_html_head "系统基本信息" >> $file_outputcreate_table_head1 >> $file_outputget_physics >>/tmp/tmpos_summ_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmpos_summ_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_html_head "cpu信息" >> $file_outputcreate_table_head1 >> $file_outputget_cpuinfo >>/tmp/tmp_cpuinfo_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmp_cpuinfo_`date +%y%m%d`.txt  create_table_end >> $file_outputcreate_html_head "ip网络信息" >> $file_outputcreate_table_head1 >> $file_outputget_netinfowhile read linedocreate_tr2 "$line" done < /tmp/tmpnet_h1_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmpnet1_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_html_head "cpu使用率" >> $file_outputcreate_table_head1 >> $file_outputget_cpuusecreate_tr3 "/tmp/tmp_cpuuse_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "连接数信息" >> $file_outputcreate_table_head1 >> $file_outputget_connections >>/tmp/tmp_connections_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmp_connections_`date +%y%m%d`.txt  create_table_end >> $file_outputcreate_html_head "系统限制信息" >> $file_outputcreate_table_head1 >> $file_outputget_ulimitinfocreate_tr3 "/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt"create_table_end >> $file_output  create_html_head "内存使用信息" >> $file_outputcreate_table_head1 >> $file_outputget_meminfowhile read linedocreate_tr1 "$line" done < /tmp/tmpmem1_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmpmem2_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_table_head1 >> $file_outputwhile read linedocreate_tr2 "$line" done < /tmp/tmpmem3_h1_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmpmem3_t1_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_html_head "磁盘使用信息" >> $file_outputcreate_table_head1 >> $file_outputget_diskinfowhile read linedocreate_tr2 "$line" done < /tmp/tmpdisk_h1_`date +%y%m%d`.txtwhile read linedocreate_tr1 "$line" done < /tmp/tmpdisk_t1_`date +%y%m%d`.txtcreate_table_end >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmpdisk1_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmpdisk2_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmpdisk3_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "网络流量情况" >> $file_outputcreate_table_head1 >> $file_outputget_topproccreate_tr3 "/tmp/tmpload_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "消耗CPU前十排行" >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmptopcpu_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "消耗内存前十排行" >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmptopmem_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "磁盘io情况" >> $file_outputcreate_table_head1 >> $file_outputcreate_tr3 "/tmp/tmptopio_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "定时任务信息" >> $file_outputcreate_table_head1 >> $file_outputget_crontablistcreate_tr3 "/tmp/tmp_crontab_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_head "定时任务脚本内容" >> $file_outputcreate_table_head1 >> $file_outputget_crontab_contentcreate_tr3 "/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txt"create_table_end >> $file_outputcreate_html_end >> $file_outputsed -i 's/BORDER=1/width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse"/g' $file_outputrm -rf /tmp/tmp*_`date +%y%m%d`.txt
}
# This script must be executed as root
RUID=`id|awk -F\( '{print $1}'|awk -F\= '{print $2}'`
if [ ${RUID} != "0" ];thenecho"This script must be executed as root"exit 1
fi
PLATFORM=`uname`
if [ ${PLATFORM} = "HP-UX" ] ; thenecho "This script does not support HP-UX platform for the time being"
exit 1
elif [ ${PLATFORM} = "SunOS" ] ; thenecho "This script does not support SunOS platform for the time being"
exit 1
elif [ ${PLATFORM} = "AIX" ] ; thenecho "This script does not support AIX platform for the time being"
exit 1
elif [ ${PLATFORM} = "Linux" ] ; thencreate_html
fi

这篇关于Linux服务器系统自动巡检脚本生成html报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1058755

相关文章

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变