前一段时间,我读到一个有效的系统管理员/工程师的一个显着特点是懒惰。 它似乎有点矛盾起初,但作者然后继续解释为什么:
RHCE系列:自动化Linux系统维护任务 - 第4部分
如果系统管理员花费大部分时间解决问题和做重复性任务,你可能会怀疑他或她做错事情。 换句话说,有效的系统管理员/工程师应该制定计划,以尽可能少地对他/她的部分执行重复性任务,并且应当预见问题,
例如,工具3部分审核- 监控系统活动报告使用Linux工具集这一系列。 因此,虽然他或她似乎没有做太多,这是因为他/她的大部分责任已经在shell脚本的帮助下处理,这是我们将在本教程中讨论的。
什么是shell脚本?
简而言之,shell脚本是一个由shell一步一步执行的程序,它是另一个在Linux内核和最终用户之间提供接口层的程序。
默认情况下,用于RHEL 7用户帐户的shell是bash(/斌/ bash)的 。 如果你想详细说明和一些历史背景,你可以参考这个维基百科文章 。
要了解更多有关的巨大组由这个shell提供的功能,你可能想看看它的手册页 ,这是在(在下载PDF格式 )。 除此之外,假设你熟悉的Linux命令(如果没有,我强烈建议你去通过一个指南从新手到系统管理员文章中youcl.com程序之前)。 现在让我们开始吧。
编写脚本以显示系统信息
为了方便起见,我们创建一个目录来存储我们的shell脚本:
# mkdir scripts # cd scripts
并打开一个新的命名文本文件system_info.sh
使用您喜欢的文本编辑器。 我们将首先在顶部插入几个注释,然后再插入一些命令:
#!/bin/bash # Sample script written for Part 4 of the RHCE series # This script will return the following set of system information: # -Hostname information: echo -e "\e[31;43m***** HOSTNAME INFORMATION *****\e[0m" hostnamectl echo "" # -File system disk space usage: echo -e "\e[31;43m***** FILE SYSTEM DISK SPACE USAGE *****\e[0m" df -h echo "" # -Free and used memory in the system: echo -e "\e[31;43m ***** FREE AND USED MEMORY *****\e[0m" free echo "" # -System uptime and load: echo -e "\e[31;43m***** SYSTEM UPTIME AND LOAD *****\e[0m" uptime echo "" # -Logged-in users: echo -e "\e[31;43m***** CURRENTLY LOGGED-IN USERS *****\e[0m" who echo "" # -Top 5 processes as far as memory usage is concerned echo -e "\e[31;43m***** TOP 5 MEMORY-CONSUMING PROCESSES *****\e[0m" ps -eo %mem,%cpu,comm --sort=-%mem | head -n 6 echo "" echo -e "\e[1;32mDone.\e[0m"
接下来,给予脚本执行权限:
# chmod +x system_info.sh
并运行它:
./system_info.sh
请注意,每个部分的标题都以颜色显示,以便更好地查看:
服务器监视Shell脚本
该功能由此命令提供:
echo -e "\e[COLOR1;COLOR2m<YOUR TEXT HERE>\e[0m"
凡COLOR1和COLOR2是前景色和背景色,分别为(详细信息和选项都从该条目解释Arch Linux的维基 )和<您的文本这里>是您要的颜色来显示字符串。
自动化任务
您可能需要自动执行的任务可能因情况而异。 因此,我们不可能在一篇文章中涵盖所有可能的场景,但我们将介绍三个可以使用shell脚本自动化的经典任务:
1)更新本地文件数据库,2)找到(或者删除)与777权限,以及3)警报时,文件系统的使用率超过规定的限值。
让我们创建一个文件名为auto_tasks.sh
在我们的脚本,内容如下目录:
#!/bin/bash # Sample script to automate tasks: # -Update local file database: echo -e "\e[4;32mUPDATING LOCAL FILE DATABASE\e[0m" updatedb if [ $? == 0 ]; then echo "The local file database was updated correctly." else echo "The local file database was not updated correctly." fi echo "" # -Find and / or delete files with 777 permissions. echo -e "\e[4;32mLOOKING FOR FILES WITH 777 PERMISSIONS\e[0m" # Enable either option (comment out the other line), but not both. # Option 1: Delete files without prompting for confirmation. Assumes GNU version of find. #find -type f -perm 0777 -delete # Option 2: Ask for confirmation before deleting files. More portable across systems. find -type f -perm 0777 -exec rm -i {} +; echo "" # -Alert when file system usage surpasses a defined limit echo -e "\e[4;32mCHECKING FILE SYSTEM USAGE\e[0m" THRESHOLD=30 while read line; do # This variable stores the file system path as a string FILESYSTEM=$(echo $line | awk '{print $1}') # This variable stores the use percentage (XX%) PERCENTAGE=$(echo $line | awk '{print $5}') # Use percentage without the % sign. USAGE=${PERCENTAGE%?} if [ $USAGE -gt $THRESHOLD ]; then echo "The remaining available space in $FILESYSTEM is critically low. Used: $PERCENTAGE" fi donePlease note that there is a space between the two
<
signs in the last line of the script.Shell Script to Find 777 Permissions
Using Cron
To take efficiency one step further, you will not want to sit in front of your computer and run those scripts manually. Rather, you will use cron to schedule those tasks to run on a periodic basis and sends the results to a predefined list of recipients via email or save them to a file that can be viewed using a web browser.
The following script (filesystem_usage.sh) will run the well-known df -h command, format the output into a HTML table and save it in the report.html file:
#!/bin/bash # Sample script to demonstrate the creation of an HTML report using shell scripting # Web directory WEB_DIR=/var/www/html # A little CSS and table layout to make the report look a little nicer echo "<HTML> <HEAD> <style> .titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;} table { border-collapse:collapse; } table, td, th { border:1px solid black; } </style> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> </HEAD> <BODY>" > $WEB_DIR/report.html # View hostname and insert it at the top of the html body HOST=$(hostname) echo "Filesystem usage for host <strong>$HOST</strong><br> Last updated: <strong>$(date)</strong><br><br> <table border='1'> <tr><th class='titulo'>Filesystem</td> <th class='titulo'>Size</td> <th class='titulo'>Use %</td> </tr>" >> $WEB_DIR/report.html # Read the output of df -h line by line while read line; do echo "<tr><td align='center'>" >> $WEB_DIR/report.html echo $line | awk '{print $1}' >> $WEB_DIR/report.html echo "</td><td align='center'>" >> $WEB_DIR/report.html echo $line | awk '{print $2}' >> $WEB_DIR/report.html echo "</td><td align='center'>" >> $WEB_DIR/report.html echo $line | awk '{print $5}' >> $WEB_DIR/report.html echo "</td></tr>" >> $WEB_DIR/report.html done < <(df -h | grep -vi filesystem) echo "</table></BODY></HTML>" >> $WEB_DIR/report.htmlIn our RHEL 7 server (192.168.0.18), this looks as follows:
Server Monitoring Report
You can add to that report as much information as you want. To run the script every day at 1:30 pm, add the following crontab entry:
30 13 * * * /root/scripts/filesystem_usage.shSummary
You will most likely think of several other tasks that you want or need to automate; as you can see, using shell scripting will greatly simplify this effort. Feel free to let us know if you find this article helpful and don't hesitate to add your own ideas or comments via the form below.
请注意,在这两个之间的空间<
在脚本的最后一行的迹象。
Shell脚本来查找777权限
使用Cron
为了进一步提高效率,您不希望坐在计算机前面并手动运行这些脚本。 相反,你会用cron来安排其定期运行这些任务,并通过电子邮件将结果发送给收件人的预定义列表或将其保存到可以使用Web浏览器查看的文件。
下面的脚本(filesystem_usage.sh)将运行知名DF -h命令,输出格式为一个HTML表,并将其保存在report.html文件:
#!/bin/bash # Sample script to demonstrate the creation of an HTML report using shell scripting # Web directory WEB_DIR=/var/www/html # A little CSS and table layout to make the report look a little nicer echo "<HTML> <HEAD> <style> .titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;} table { border-collapse:collapse; } table, td, th { border:1px solid black; } </style> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> </HEAD> <BODY>" > $WEB_DIR/report.html # View hostname and insert it at the top of the html body HOST=$(hostname) echo "Filesystem usage for host <strong>$HOST</strong><br> Last updated: <strong>$(date)</strong><br><br> <table border='1'> <tr><th class='titulo'>Filesystem</td> <th class='titulo'>Size</td> <th class='titulo'>Use %</td> </tr>" >> $WEB_DIR/report.html # Read the output of df -h line by line while read line; do echo "<tr><td align='center'>" >> $WEB_DIR/report.html echo $line | awk '{print $1}' >> $WEB_DIR/report.html echo "</td><td align='center'>" >> $WEB_DIR/report.html echo $line | awk '{print $2}' >> $WEB_DIR/report.html echo "</td><td align='center'>" >> $WEB_DIR/report.html echo $line | awk '{print $5}' >> $WEB_DIR/report.html echo "</td></tr>" >> $WEB_DIR/report.html done < <(df -h | grep -vi filesystem) echo "</table></BODY></HTML>" >> $WEB_DIR/report.html
在我们的RHEL 7服务器(192.168.0.18),这看起来如下:
服务器监控报告
您可以根据需要向该报告添加尽可能多的信息。 到下午1:30,每天运行脚本,添加以下crontab条目:
30 13 * * * /root/scripts/filesystem_usage.sh
概要
你很可能会想到你想要或需要自动化的几个其他任务; 正如你所看到的,使用shell脚本将大大简化这种努力。 如果您觉得这篇文章有帮助,欢迎随时与我们联系,不要犹豫,通过下面的表格添加您自己的想法或评论。