• 通过shell检测linux环境并导出HTML页面信息


    1. #!/bin/sh
    2. ipaddress=`ip a|grep "global"|awk '{print $2}' |awk -F/ '{print $1}'`
    3. file_output='os_linux_summary.html'
    4. td_str=''
    5. th_str=''
    6. #yum -y install bc sysstat net-tools
    7. create_html_css(){
    8. echo -e "
    9. "
    10. }
    11. create_html_head(){
    12. echo -e "

      $1

      "
    13. }
    14. create_table_head1(){
    15. echo -e "" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
    16. }
    17. create_table_head2(){
    18. echo -e "
    19. " border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
    20. }
    21. create_td(){
    22. td_str=`echo $1 | awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "
    23. ";i++}}'`
    24. }
    25. create_th(){
    26. th_str=`echo $1|awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "
    27. ";i++}}'`
    28. }
    29. create_tr1(){
    30. create_td "$1"
    31. echo -e "
    32. $td_str
    33. " >> $file_output
    34. }
    35. create_tr2(){
    36. create_th "$1"
    37. echo -e "
    38. $th_str
    39. " >> $file_output
    40. }
    41. create_tr3(){
    42. echo -e "
    43. " >> $file_output
    44. }
    45. create_table_end(){
    46. echo -e "
    47. "$i""$i"
    48. `cat $1`
    49. "
    50. }
    51. create_html_end(){
    52. echo -e ""
    53. }
    54. NAME_VAL_LEN=12
    55. name_val () {
    56. printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2"
    57. }
    58. get_physics(){
    59. name_val "巡检时间" "`date`"
    60. name_val "主机名" "`uname -n`"
    61. name_val "系统版本" "`cat /etc/{oracle,redhat,SuSE,centos}-release 2>/dev/null|sort -ru|head -n1`"
    62. name_val "内核版本" "`uname -r`"
    63. name_val "架构" "CPU=`lscpu|grep Architecture|awk -F: '{print $2}'|sed 's/^[[:space:]]*//g'`;OS=`getconf LONG_BIT`-bit"
    64. }
    65. get_cpuinfo () {
    66. file="/proc/cpuinfo"
    67. virtual=`grep -c ^processor "${file}"`
    68. physical=`grep 'physical id' "${file}" | sort -u | wc -l`
    69. cores=`grep 'cpu cores' "${file}" | head -n 1 | cut -d: -f2`
    70. model=`grep "model name" "${file}"|sort -u|awk -F: '{print $2}'`
    71. speed=`grep -i "cpu MHz" "${file}"|sort -u|awk -F: '{print $2}'`
    72. cache=`grep -i "cache size" "${file}"|sort -u|awk -F: '{print $2}'`
    73. SysCPUIdle=`vmstat | sed -n '$ p' | awk '{print $15}'`
    74. [ "${physical}" = "0" ] && physical="${virtual}"
    75. [ -z "${cores}" ] && cores=0
    76. cores=$((${cores} * ${physical}));
    77. htt=""
    78. if [ ${cores} -gt 0 -a $cores -lt $virtual ]; then htt=yes; else htt=no; fi
    79. name_val "线程" "physical = ${physical}, cores = ${cores}, virtual = ${virtual}, hyperthreading = ${htt}"
    80. name_val "cpu型号" "${physical}*${model}"
    81. name_val "速度" "${virtual}*${speed}"
    82. name_val "缓存" "${virtual}*${cache}"
    83. name_val "CPU空闲率(%)" "${SysCPUIdle}%"
    84. }
    85. get_netinfo(){
    86. echo "interface | status | ipadds | mtu | Speed | Duplex" >>/tmp/tmpnet_h1_`date +%y%m%d`.txt
    87. for ipstr in `ifconfig -a|grep ": flags"|awk '{print $1}'|sed 's/.$//'`
    88. do
    89. ipadds=`ifconfig ${ipstr}|grep -w inet|awk '{print $2}'`
    90. mtu=`ifconfig ${ipstr}|grep mtu|awk '{print $NF}'`
    91. speed=`ethtool ${ipstr}|grep Speed|awk -F: '{print $2}'`
    92. duplex=`ethtool ${ipstr}|grep Duplex|awk -F: '{print $2}'`
    93. echo "${ipstr}" "up" "${ipadds}" "${mtu}" "${speed}" "${duplex}"\
    94. |awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6}' >>/tmp/tmpnet1_`date +%y%m%d`.txt
    95. done
    96. }
    97. get_cpuuse(){
    98. echo "####################################### cpu使用率 #######################################" >>/tmp/tmp_cpuuse_`date +%y%m%d`.txt
    99. mpstat -P ALL 10 6 >>/tmp/tmp_cpuuse_`date +%y%m%d`.txt
    100. }
    101. get_connections (){
    102. filemax=`cat /proc/sys/fs/file-max`
    103. name_val "Number of concurrent connections" "${filemax}"
    104. }
    105. get_ulimitinfo(){
    106. echo "####################################### 系统限制最大进程数 #######################################" >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
    107. ulimit -a >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
    108. }
    109. get_meminfo(){
    110. echo "Locator |Size |Speed |Form Factor | Type | Type Detail" >>/tmp/tmpmem3_h1_`date +%y%m%d`.txt
    111. dmidecode| grep -v "Memory Device Mapped Address"|grep -A12 -w "Memory Device" \
    112. |egrep "Locator:|Size:|Speed:|Form Factor:|Type:|Type Detail:" \
    113. |awk -F: '/Size|Type|Form.Factor|Type.Detail|^[\t ]+Locator/{printf("|%s", $2)}/^[\t ]+Speed/{print "|" $2}' \
    114. |grep -v "No Module Installed" \
    115. |awk -F"|" '{print $4,"|", $2,"|", $7,"|", $3,"|", $5,"|", $6}' >>/tmp/tmpmem3_t1_`date +%y%m%d`.txt
    116. free -glht >>/tmp/tmpmem2_`date +%y%m%d`.txt
    117. memtotal=`vmstat -s | head -1 | awk '{print $1}'`
    118. avm=`vmstat -s| sed -n '3p' | awk '{print $1}'`
    119. name_val "Mem_used_rate(%)" "`echo "100*${avm}/${memtotal}" | bc`%" >>/tmp/tmpmem1_`date +%y%m%d`.txt
    120. }
    121. get_diskinfo(){
    122. echo "Filesystem |Type |Size | Used | Avail | Use% | Mounted on | Opts" >>/tmp/tmpdisk_h1_`date +%y%m%d`.txt
    123. df -ThP|grep -v tmpfs|sed '1d'|sort >/tmp/tmpdf1_`date +%y%m%d`.txt
    124. mount -l|awk '{print $1,$6}'|grep ^/|sort >/tmp/tmpdf2_`date +%y%m%d`.txt
    125. join /tmp/tmpdf1_`date +%y%m%d`.txt /tmp/tmpdf2_`date +%y%m%d`.txt\
    126. |awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6,"|", $7,"|", $8}' >>/tmp/tmpdisk_t1_`date +%y%m%d`.txt
    127. lsblk >>/tmp/tmpdisk1_`date +%y%m%d`.txt
    128. for disk in `ls -l /sys/block|awk '{print $9}'|sed '/^$/d'|grep -v fd`
    129. do
    130. echo "${disk}" `cat /sys/block/${disk}/queue/scheduler` >>/tmp/tmpdisk2_`date +%y%m%d`.txt
    131. done
    132. pvs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
    133. echo "====================== ===== ===== ===== ===== ===== ========== =======" >>/tmp/tmpdisk3_`date +%y%m%d`.txt
    134. vgs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
    135. echo "====================== ===== ===== ===== ===== ===== ========== =======" >>/tmp/tmpdisk3_`date +%y%m%d`.txt
    136. lvs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
    137. }
    138. get_topproc(){
    139. #os load
    140. echo "####################################### 网络流量情况 #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
    141. sar -n DEV 10 6 >>/tmp/tmpload_`date +%y%m%d`.txt
    142. echo "####################################### 系统资源变化 #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
    143. vmstat -S M 10 6 >>/tmp/tmpload_`date +%y%m%d`.txt
    144. #top cpu
    145. mpstat 1 5 >>/tmp/tmptopcpu_`date +%y%m%d`.txt
    146. echo "####################################### 消耗CPU前十排行 #######################################" >>/tmp/tmptopcpu_`date +%y%m%d`.txt
    147. ps aux|head -1 >>/tmp/tmptopcpu_`date +%y%m%d`.txt
    148. ps aux|grep -v PID|sort -rn -k +3|head >>/tmp/tmptopcpu_`date +%y%m%d`.txt
    149. #top mem
    150. echo "####################################### 消耗内存前十排行 #######################################" >>/tmp/tmptopmem_`date +%y%m%d`.txt
    151. ps aux|head -1 >>/tmp/tmptopmem_`date +%y%m%d`.txt
    152. ps aux|grep -v PID|sort -rn -k +4|head >>/tmp/tmptopmem_`date +%y%m%d`.txt
    153. echo "TOP10 CPU Resource Process" >>/tmp/tmptopmem_`date +%y%m%d`.txt
    154. top -bn1 -o "%CPU"|sed -n '1,17p'
    155. #top i/o
    156. echo "####################################### 磁盘io情况 #######################################" >>/tmp/tmptopio_`date +%y%m%d`.txt
    157. iostat -k -d 10 5 >>/tmp/tmptopio_`date +%y%m%d`.txt
    158. }
    159. get_crontablist(){
    160. crontab -l >>/tmp/tmp_crontab_`date +%y%m%d`.txt
    161. if [ -s /tmp/tmp_crontab_`date +%y%m%d`.txt ] ; then
    162. echo 'ths file is not empyt and file info'
    163. else
    164. echo '#### 无定时任务 ####' >>/tmp/tmp_crontab_`date +%y%m%d`.txt
    165. fi
    166. }
    167. create_html(){
    168. rm -rf $file_output
    169. touch $file_output
    170. create_html_css >> $file_output
    171. create_html_head "系统基本信息" >> $file_output
    172. create_table_head1 >> $file_output
    173. get_physics >>/tmp/tmpos_summ_`date +%y%m%d`.txt
    174. while read line
    175. do
    176. create_tr1 "$line"
    177. done < /tmp/tmpos_summ_`date +%y%m%d`.txt
    178. create_table_end >> $file_output
    179. create_html_head "cpu信息" >> $file_output
    180. create_table_head1 >> $file_output
    181. get_cpuinfo >>/tmp/tmp_cpuinfo_`date +%y%m%d`.txt
    182. while read line
    183. do
    184. create_tr1 "$line"
    185. done < /tmp/tmp_cpuinfo_`date +%y%m%d`.txt
    186. create_table_end >> $file_output
    187. create_html_head "ip网络信息" >> $file_output
    188. create_table_head1 >> $file_output
    189. get_netinfo
    190. while read line
    191. do
    192. create_tr2 "$line"
    193. done < /tmp/tmpnet_h1_`date +%y%m%d`.txt
    194. while read line
    195. do
    196. create_tr1 "$line"
    197. done < /tmp/tmpnet1_`date +%y%m%d`.txt
    198. create_table_end >> $file_output
    199. create_html_head "cpu使用率" >> $file_output
    200. create_table_head1 >> $file_output
    201. get_cpuuse
    202. create_tr3 "/tmp/tmp_cpuuse_`date +%y%m%d`.txt"
    203. create_table_end >> $file_output
    204. create_html_head "连接数信息" >> $file_output
    205. create_table_head1 >> $file_output
    206. get_connections >>/tmp/tmp_connections_`date +%y%m%d`.txt
    207. while read line
    208. do
    209. create_tr1 "$line"
    210. done < /tmp/tmp_connections_`date +%y%m%d`.txt
    211. create_table_end >> $file_output
    212. create_html_head "系统限制信息" >> $file_output
    213. create_table_head1 >> $file_output
    214. get_ulimitinfo
    215. create_tr3 "/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt"
    216. create_table_end >> $file_output
    217. create_html_head "内存使用信息" >> $file_output
    218. create_table_head1 >> $file_output
    219. get_meminfo
    220. while read line
    221. do
    222. create_tr1 "$line"
    223. done < /tmp/tmpmem1_`date +%y%m%d`.txt
    224. create_table_end >> $file_output
    225. create_table_head1 >> $file_output
    226. create_tr3 "/tmp/tmpmem2_`date +%y%m%d`.txt"
    227. create_table_end >> $file_output
    228. create_table_head1 >> $file_output
    229. while read line
    230. do
    231. create_tr2 "$line"
    232. done < /tmp/tmpmem3_h1_`date +%y%m%d`.txt
    233. while read line
    234. do
    235. create_tr1 "$line"
    236. done < /tmp/tmpmem3_t1_`date +%y%m%d`.txt
    237. create_table_end >> $file_output
    238. create_html_head "磁盘使用信息" >> $file_output
    239. create_table_head1 >> $file_output
    240. get_diskinfo
    241. while read line
    242. do
    243. create_tr2 "$line"
    244. done < /tmp/tmpdisk_h1_`date +%y%m%d`.txt
    245. while read line
    246. do
    247. create_tr1 "$line"
    248. done < /tmp/tmpdisk_t1_`date +%y%m%d`.txt
    249. create_table_end >> $file_output
    250. create_table_head1 >> $file_output
    251. create_tr3 "/tmp/tmpdisk1_`date +%y%m%d`.txt"
    252. create_table_end >> $file_output
    253. create_table_head1 >> $file_output
    254. create_tr3 "/tmp/tmpdisk2_`date +%y%m%d`.txt"
    255. create_table_end >> $file_output
    256. create_table_head1 >> $file_output
    257. create_tr3 "/tmp/tmpdisk3_`date +%y%m%d`.txt"
    258. create_table_end >> $file_output
    259. create_html_head "网络流量情况" >> $file_output
    260. create_table_head1 >> $file_output
    261. get_topproc
    262. create_tr3 "/tmp/tmpload_`date +%y%m%d`.txt"
    263. create_table_end >> $file_output
    264. create_html_head "消耗CPU前十排行" >> $file_output
    265. create_table_head1 >> $file_output
    266. create_tr3 "/tmp/tmptopcpu_`date +%y%m%d`.txt"
    267. create_table_end >> $file_output
    268. create_html_head "消耗内存前十排行" >> $file_output
    269. create_table_head1 >> $file_output
    270. create_tr3 "/tmp/tmptopmem_`date +%y%m%d`.txt"
    271. create_table_end >> $file_output
    272. create_html_head "磁盘io情况" >> $file_output
    273. create_table_head1 >> $file_output
    274. create_tr3 "/tmp/tmptopio_`date +%y%m%d`.txt"
    275. create_table_end >> $file_output
    276. create_html_head "定时任务信息" >> $file_output
    277. create_table_head1 >> $file_output
    278. get_crontablist
    279. create_tr3 "/tmp/tmp_crontab_`date +%y%m%d`.txt"
    280. create_table_end >> $file_output
    281. create_html_end >> $file_output
    282. sed -i 's/BORDER=1/width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse"/g' $file_output
    283. rm -rf /tmp/tmp*_`date +%y%m%d`.txt
    284. }
    285. # This script must be executed as root
    286. RUID=`id|awk -F\( '{print $1}'|awk -F\= '{print $2}'`
    287. if [ ${RUID} != "0" ];then
    288. echo"This script must be executed as root"
    289. exit 1
    290. fi
    291. PLATFORM=`uname`
    292. if [ ${PLATFORM} = "HP-UX" ] ; then
    293. echo "This script does not support HP-UX platform for the time being"
    294. exit 1
    295. elif [ ${PLATFORM} = "SunOS" ] ; then
    296. echo "This script does not support SunOS platform for the time being"
    297. exit 1
    298. elif [ ${PLATFORM} = "AIX" ] ; then
    299. echo "This script does not support AIX platform for the time being"
    300. exit 1
    301. elif [ ${PLATFORM} = "Linux" ] ; then
    302. create_html
    303. fi

    linux启动脚本,会生成 os_linux_summary.html文件

  • 相关阅读:
    Next.js 热更新 Markdown 文件变更
    TypeScript基础常用知识点总结
    基于深度学习的图像生成与识别技术研究
    telnet 连接闪退
    temporal shift module(TSM)
    Twitter群推解锁流量大门的钥匙
    Blender 插件
    聊聊流式数据湖Paimon(三)
    python-Matplotlib画图那些你不知道的事
    RHCE(五)HTTP、SSL协议综合实验
  • 原文地址:https://blog.csdn.net/qq_37989070/article/details/133052077