生成Linux运行时间报告的Bash脚本
出于一些原因,你可能需要每月收集一次 Linux 系统运行时间报告。如果是这样,你可以根据需要使用以下 bash 脚本 之一。
我们为什么要收集这份报告?在一段时间后重启 Linux 服务器是解决某些未解决问题的好方法。(LCTT 译注:本文这些观点值得商榷,很多服务器可以稳定运行几千天,尤其是有了内核热补丁之后,启动并不是必须的。)
建议每 180 天重新启动一次。但时间段也许取决于你公司的政策。如果你已经长时间运行服务器而没有重启。这可能导致服务器上出现一些性能或内存问题,我在许多服务器上都注意到了这一点。
这些脚本一次性提供了所有系统运行报告。
什么是 uptime 命令
uptime
命令将告诉你系统已经运行了多长时间。它在一行中显示以下信息:当前时间、系统运行了多长时间、当前登录了多少用户以及过去 1、5 和 15 分钟的平均系统负载。
什么是 tuptime?
tuptime 是用于报告系统的历史和统计运行时间的工具,可在重启之间保存。它类似于 uptime
命令,但输出更有趣。
1)检查 Linux 系统运行时间的 Bash 脚本
该 bash 脚本将收集所有服务器正常运行时间,并将报告发送到给定的电子邮箱地址。
请替换为你的电子邮箱地址,而不是用我们的,否则你将不会收到邮件。
<span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">system</span><span class="pun">-</span><span class="kwd">uptime</span><span class="pun">-</span><span class="kwd">script</span><span class="pun">.</span><span class="pln">sh</span>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="pun">></span><span class="pln"> </span><span class="str">/tmp/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">.</span><span class="pln">out</span>
<span class="kwd">for</span><span class="pln"> host </span><span class="kwd">in</span><span class="pln"> </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">servers</span><span class="pun">.</span><span class="pln">txt</span>
<span class="kwd">do</span>
<span class="kwd">echo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n </span><span class="str">"$host: "</span>
<span class="kwd">ssh</span><span class="pln"> $host </span><span class="kwd">uptime</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $3,$4}'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">sed</span><span class="pln"> </span><span class="str">'s/,//'</span>
<span class="kwd">done</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> column </span><span class="pun">-</span><span class="pln">t </span><span class="pun">>></span><span class="pln"> </span><span class="str">/tmp/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">.</span><span class="pln">out</span>
<span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">.</span><span class="pln">out </span><span class="pun">|</span><span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Linux Servers Uptime Report"</span><span class="pln"> </span><span class="str">"[email protected]"</span>
给 system-uptime-script.sh
设置可执行权限。
<span class="pln">$ </span><span class="kwd">chmod</span><span class="pln"> </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">system</span><span class="pun">-</span><span class="kwd">uptime</span><span class="pun">-</span><span class="kwd">script</span><span class="pun">.</span><span class="pln">sh</span>
最后运行 bash 脚本获取输出。
<span class="com">#</span><span class="pln"> sh </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">system</span><span class="pun">-</span><span class="kwd">uptime</span><span class="pun">-</span><span class="kwd">script</span><span class="pun">.</span><span class="pln">sh</span>
你将收到类似以下的报告。
<span class="com">#</span><span class="pln"> </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">.</span><span class="pln">out</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.5</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> days</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.6</span><span class="pun">:</span><span class="pln"> </span><span class="lit">15</span><span class="pln"> days</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.7</span><span class="pun">:</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> days</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.8</span><span class="pun">:</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> days</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.9</span><span class="pun">:</span><span class="pln"> </span><span class="lit">67</span><span class="pln"> days</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.10</span><span class="pun">:</span><span class="pln"> </span><span class="lit">130</span><span class="pln"> days</span>
<span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.11</span><span class="pun">:</span><span class="pln"> </span><span class="lit">23</span><span class="pln"> days</span>
2)检查 Linux 系统是否运行了 30 天以上的 Bash 脚本
此 bash 脚本会收集运行 30 天以上的服务器,并将报告发送到指定的邮箱地址。你可以根据需要更改天数。
<span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">system</span><span class="pun">-</span><span class="kwd">uptime</span><span class="pun">-</span><span class="kwd">script</span><span class="pun">-</span><span class="lit">1.sh</span>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="pun">></span><span class="pln"> </span><span class="str">/tmp/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">-</span><span class="lit">1.out</span><span class="pln"> </span>
<span class="kwd">for</span><span class="pln"> host </span><span class="kwd">in</span><span class="pln"> </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">servers</span><span class="pun">.</span><span class="pln">txt</span>
<span class="kwd">do</span>
<span class="kwd">echo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n </span><span class="str">"$host: "</span>
<span class="kwd">ssh</span><span class="pln"> $host </span><span class="kwd">uptime</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $3,$4}'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">sed</span><span class="pln"> </span><span class="str">'s/,//'</span>
<span class="kwd">done</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> column </span><span class="pun">-</span><span class="pln">t </span><span class="pun">>></span><span class="pln"> </span><span class="str">/tmp/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">-</span><span class="lit">1.out</span>
<span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">-</span><span class="lit">1.out</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' $2 >= 30'</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="str">/tmp/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">-</span><span class="lit">2.out</span>
<span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">report</span><span class="pun">-</span><span class="lit">2.out</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Linux Servers Uptime Report"</span><span class="pln"> </span><span class="str">"[email protected]"</span>
给 system-uptime-script-1.sh
设置可执行权限。
<span class="pln">$ </span><span class="kwd">chmod</span><span class="pln"> </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">system</span><span class="pun">-</span><span class="kwd">uptime</span><span class="pun">-</span><span class="kwd">script</span><span class="pun">-</span><span class="lit">1.sh</span>
最后添加一条 cronjob 来自动执行。它会在每天早上 7 点运行。
<span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class="pln">e</span>
<span class="lit">0</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="str">/bin/</span><span class="kwd">bash</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">system</span><span class="pun">-</span><span class="kwd">uptime</span><span class="pun">-</span><span class="kwd">script</span><span class="pun">-</span><span class="lit">1.sh</span>
注意: 你会在每天早上 7 点会收到一封电子邮件提醒,它是昨天的详情。