RHCE 系列(四): 使用 Shell 脚本自动化 Linux 系统维护任务
之前我听说高效的系统管理员的一个特点是懒惰。一开始看起来很矛盾,但作者接下来解释了其中的原因:
RHCE 系列:第四部分 - 自动化 Linux 系统维护任务
如果一个系统管理员花费大量的时间解决问题以及做重复的工作,你就应该怀疑他这么做是否正确。换句话说,一个高效的系统管理员/工程师应该制定一个计划使得其尽量花费少的时间去做重复的工作,以及通过使用本系列中第三部分 使用 Linux 工具集监视系统活动报告 介绍的工具来预见问题。因此,尽管看起来他/她没有做很多的工作,但那是因为 shell 脚本帮助完成了他的/她的大部分任务,这也就是本章我们将要探讨的东西。
什么是 shell 脚本?
简单的说,shell 脚本就是一个由 shell 一步一步执行的程序,而 shell 是在 Linux 内核和最终用户之间提供接口的另一个程序。
默认情况下,RHEL 7 中用户使用的 shell 是 bash(/bin/bash)。如果你想知道详细的信息和历史背景,你可以查看这个维基页面。
关于这个 shell 提供的众多功能的介绍,可以查看 man 手册,也可以从 (Bash 命令)处下载 PDF 格式。除此之外,假设你已经熟悉 Linux 命令(否则我强烈建议你首先看一下 Tecmint.com 中的文章 从新手到系统管理员指南 )。现在让我们开始吧。
写一个脚本显示系统信息
为了方便,首先让我们新建一个目录用于保存我们的 shell 脚本:
<span class="com">#</span><span class="kwd">mkdir</span><span class="pln"> scripts</span>
<span class="com">#</span><span class="kwd">cd</span><span class="pln"> scripts</span>
然后用喜欢的文本编辑器打开新的文本文件 system_info.sh
。我们首先在头部插入一些注释以及一些命令:
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="com">#</span><span class="pln"> RHCE </span><span class="pun">系列第四部分示例脚本</span>
<span class="com">#</span><span class="pun">该脚本会返回以下这些系统信息:</span>
<span class="com">#</span><span class="pun">-主机名称:</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[31;43m***** HOSTNAME INFORMATION *****\e[0m"</span>
<span class="kwd">hostnamectl</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-文件系统磁盘空间使用:</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[31;43m***** FILE SYSTEM DISK SPACE USAGE *****\e[0m"</span>
<span class="kwd">df</span><span class="pun">-</span><span class="pln">h</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-系统空闲和使用中的内存:</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[31;43m ***** FREE AND USED MEMORY *****\e[0m"</span>
<span class="pln">free</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-系统启动时间:</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[31;43m***** SYSTEM UPTIME AND LOAD *****\e[0m"</span>
<span class="kwd">uptime</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-登录的用户:</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[31;43m***** CURRENTLY LOGGED-IN USERS *****\e[0m"</span>
<span class="kwd">who</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-使用内存最多的</span><span class="lit">5</span><span class="pun">个进程</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[31;43m***** TOP 5 MEMORY-CONSUMING PROCESSES *****\e[0m"</span>
<span class="kwd">ps</span><span class="pun">-</span><span class="pln">eo </span><span class="pun">%</span><span class="pln">mem</span><span class="pun">,%</span><span class="pln">cpu</span><span class="pun">,</span><span class="pln">comm </span><span class="pun">--</span><span class="kwd">sort</span><span class="pun">=-%</span><span class="pln">mem </span><span class="pun">|</span><span class="kwd">head</span><span class="pun">-</span><span class="pln">n </span><span class="lit">6</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[1;32mDone.\e[0m"</span>
然后,给脚本可执行权限:
<span class="com">#</span><span class="kwd">chmod</span><span class="pun">+</span><span class="pln">x system_info</span><span class="pun">.</span><span class="pln">sh</span>
运行脚本:
<span class="pun">./</span><span class="pln">system_info</span><span class="pun">.</span><span class="pln">sh</span>
注意为了更好的可视化效果各部分标题都用颜色显示:
服务器监视 Shell 脚本
颜色功能是由以下命令提供的:
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[COLOR1;COLOR2m<YOUR TEXT HERE>\e[0m"</span>
其中 COLOR1 和 COLOR2 是前景色和背景色(Arch Linux Wiki 有更多的信息和选项解释), 是你想用颜色显示的字符串。
使任务自动化
你想使其自动化的任务可能因情况而不同。因此,我们不可能在一篇文章中覆盖所有可能的场景,但是我们会介绍使用 shell 脚本可以使其自动化的三种典型任务:
1) 更新本地文件数据库, 2) 查找(或者删除)有 777 权限的文件, 以及 3) 文件系统使用超过定义的阀值时发出警告。
让我们在脚本目录中新建一个名为 auto_tasks.sh
的文件并添加以下内容:
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="com">#</span><span class="pun">自动化任务示例脚本:</span>
<span class="com">#</span><span class="pun">-更新本地文件数据库:</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[4;32mUPDATING LOCAL FILE DATABASE\e[0m"</span>
<span class="pln">updatedb</span>
<span class="kwd">if</span><span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span><span class="pun">==</span><span class="lit">0</span><span class="pun">];</span><span class="kwd">then</span>
<span class="kwd">echo</span><span class="str">"The local file database was updated correctly."</span>
<span class="kwd">else</span>
<span class="kwd">echo</span><span class="str">"The local file database was not updated correctly."</span>
<span class="kwd">fi</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-查找</span><span class="pun">和/或</span><span class="pun">删除有</span><span class="lit">777</span><span class="pun">权限的文件。</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[4;32mLOOKING FOR FILES WITH 777 PERMISSIONS\e[0m"</span>
<span class="com">#</span><span class="typ">Enable</span><span class="pln"> either option </span><span class="pun">(</span><span class="pln">comment out the other line</span><span class="pun">),</span><span class="pln"> but </span><span class="kwd">not</span><span class="pln"> both</span><span class="pun">.</span>
<span class="com">#</span><span class="typ">Option</span><span class="lit">1</span><span class="pun">:</span><span class="typ">Delete</span><span class="pln"> files without prompting </span><span class="kwd">for</span><span class="pln"> confirmation</span><span class="pun">.</span><span class="typ">Assumes</span><span class="pln"> GNU version of </span><span class="kwd">find</span><span class="pun">.</span>
<span class="com">#</span><span class="kwd">find</span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">perm </span><span class="lit">0777</span><span class="pun">-</span><span class="kwd">delete</span>
<span class="com">#</span><span class="typ">Option</span><span class="lit">2</span><span class="pun">:</span><span class="typ">Ask</span><span class="kwd">for</span><span class="pln"> confirmation before deleting files</span><span class="pun">.</span><span class="typ">More</span><span class="pln"> portable across systems</span><span class="pun">.</span>
<span class="kwd">find</span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">perm </span><span class="lit">0777</span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">rm</span><span class="pun">-</span><span class="pln">i </span><span class="pun">{}</span><span class="pun">+;</span>
<span class="kwd">echo</span><span class="str">""</span>
<span class="com">#</span><span class="pun">-文件系统使用率超过定义的阀值时发出警告</span>
<span class="kwd">echo</span><span class="pun">-</span><span class="pln">e </span><span class="str">"\e[4;32mCHECKING FILE SYSTEM USAGE\e[0m"</span>
<span class="pln">THRESHOLD</span><span class="pun">=</span><span class="lit">30</span>
<span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span><span class="kwd">do</span>
<span class="com">#</span><span class="typ">This</span><span class="pln"> variable stores the </span><span class="kwd">file</span><span class="pln"> system path </span><span class="kwd">as</span><span class="pln"> a string</span>
<span class="pln">FILESYSTEM</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">echo</span><span class="pln"> $line </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $1}'</span><span class="pun">)</span>
<span class="com">#</span><span class="typ">This</span><span class="pln"> variable stores the </span><span class="kwd">use</span><span class="pln"> percentage </span><span class="pun">(</span><span class="pln">XX</span><span class="pun">%)</span>
<span class="pln">PERCENTAGE</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">echo</span><span class="pln"> $line </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $5}'</span><span class="pun">)</span>
<span class="com">#</span><span class="typ">Use</span><span class="pln"> percentage without the </span><span class="pun">%</span><span class="pln"> sign</span><span class="pun">.</span>
<span class="pln">USAGE</span><span class="pun">=</span><span class="pln">$</span><span class="pun">{</span><span class="pln">PERCENTAGE</span><span class="pun">%?}</span>
<span class="kwd">if</span><span class="pun">[</span><span class="pln"> $USAGE </span><span class="pun">-</span><span class="pln">gt $THRESHOLD </span><span class="pun">];</span><span class="kwd">then</span>
<span class="kwd">echo</span><span class="str">"The remaining available space in $FILESYSTEM is critically low. Used: $PERCENTAGE"</span>
<span class="kwd">fi</span>
<span class="kwd">done</span><span class="pun"><</span><span class="pun"><(</span><span class="kwd">df</span><span class="pun">-</span><span class="pln">h </span><span class="pun">--</span><span class="pln">total </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">-</span><span class="kwd">vi</span><span class="pln"> filesystem</span><span class="pun">)</span>
请注意该脚本最后一行两个 <
符号之间有个空格。
查找 777 权限文件的 Shell 脚本
使用 Cron
想更进一步提高效率,你不会想只是坐在你的电脑前手动执行这些脚本。相反,你会使用 cron 来调度这些任务周期性地执行,并把结果通过邮件发动给预先指定的接收者,或者将它们保存到使用 web 浏览器可以查看的文件中。
下面的脚本(filesystem_usage.sh)会运行有名的 df -h 命令,格式化输出到 HTML 表格并保存到 report.html 文件中:
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="com">#</span><span class="pun">演示使用</span><span class="pln"> shell </span><span class="pun">脚本创建</span><span class="pln"> HTML </span><span class="pun">报告的示例脚本</span>
<span class="com">#</span><span class="typ">Web</span><span class="pln"> directory</span>
<span class="pln">WEB_DIR</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span>
<span class="com">#</span><span class="pln"> A little CSS </span><span class="kwd">and</span><span class="pln"> table layout to </span><span class="kwd">make</span><span class="pln"> the report </span><span class="kwd">look</span><span class="pln"> a little nicer</span>
<span class="kwd">echo</span><span class="str">"<HTML></span>
<span class="str"><HEAD></span>
<span class="str"><style></span>
<span class="str">.titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;}</span>
<span class="str">table</span>
<span class="str">{</span>
<span class="str">border-collapse:collapse;</span>
<span class="str">}</span>
<span class="str">table, td, th</span>
<span class="str">{</span>
<span class="str">border:1px solid black;</span>
<span class="str">}</span>
<span class="str"></style></span>
<span class="str"><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></span>
<span class="str"></HEAD></span>
<span class="str"><BODY>"</span><span class="pun">></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="com">#</span><span class="typ">View</span><span class="kwd">hostname</span><span class="kwd">and</span><span class="pln"> insert it at the </span><span class="kwd">top</span><span class="pln"> of the html body</span>
<span class="pln">HOST</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">hostname</span><span class="pun">)</span>
<span class="kwd">echo</span><span class="str">"Filesystem usage for host <strong>$HOST</strong><br></span>
<span class="str">Last updated: <strong>$(date)</strong><br><br></span>
<span class="str"><table border='1'></span>
<span class="str"><tr><th class='titulo'>Filesystem</td></span>
<span class="str"><th class='titulo'>Size</td></span>
<span class="str"><th class='titulo'>Use %</td></span>
<span class="str"></tr>"</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="com">#</span><span class="typ">Read</span><span class="pln"> the output of </span><span class="kwd">df</span><span class="pun">-</span><span class="pln">h line by line</span>
<span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span><span class="kwd">do</span>
<span class="kwd">echo</span><span class="str">"<tr><td align='center'>"</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">echo</span><span class="pln"> $line </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $1}'</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">echo</span><span class="str">"</td><td align='center'>"</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">echo</span><span class="pln"> $line </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $2}'</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">echo</span><span class="str">"</td><td align='center'>"</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">echo</span><span class="pln"> $line </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $5}'</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">echo</span><span class="str">"</td></tr>"</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
<span class="kwd">done</span><span class="pun"><</span><span class="pun"><(</span><span class="kwd">df</span><span class="pun">-</span><span class="pln">h </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">-</span><span class="kwd">vi</span><span class="pln"> filesystem</span><span class="pun">)</span>
<span class="kwd">echo</span><span class="str">"</table></BODY></HTML>"</span><span class="pun">>></span><span class="pln"> $WEB_DIR</span><span class="pun">/</span><span class="pln">report</span><span class="pun">.</span><span class="pln">html</span>
在我们的 RHEL 7 服务器(192.168.0.18)中,看起来像下面这样:
服务器监视报告
你可以添加任何你想要的信息到那个报告中。添加下面的 crontab 条目在每天下午的 1:30 运行该脚本:
<span class="lit">30</span><span class="lit">13</span><span class="pun">*</span><span class="pun">*</span><span class="pun">*</span><span class="str">/root/</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">filesystem_usage</span><span class="pun">.</span><span class="pln">sh</span>
总结
你很可能想起各种其他想要自动化的任务;正如你看到的,使用 shell 脚本能极大的简化任务。如果你觉得这篇文章对你有所帮助就告诉我们吧,别犹豫在下面的表格中添加你自己的想法或评论。
via: http://www.tecmint.com/using-shell-script-to-automate-linux-system-maintenance-tasks/
作者:Gabriel Cánepa 译者:ictlyh 校对:wxy