Iji Shell Scripting rụkwaa ọrụ nlekọta sistemu Linux - Nkebi 4


Oge ụfọdụ gara aga, agụọ m na otu n'ime njirimara njirimara nke onye nchịkwa/injinia dị irè bụ ịdị umengwụ. Ọ dị ka ọ na-emegiderịta onwe ya na mbụ mana onye dere ya gara n'ihu ịkọwa ihe kpatara ya:

Ọ bụrụ na sysadmin na-etinye ọtụtụ oge ya na-edozi nsogbu na ịrụ ọrụ ugboro ugboro, ị nwere ike chee na ọ naghị eme ihe nke ọma. N'ikwu ya n'ụzọ ọzọ, onye na-ahụ maka sistemụ na-arụ ọrụ/injinia kwesịrị ịmepụta atụmatụ iji rụọ ọrụ ugboro ugboro na obere ihe na akụkụ ya dị ka o kwere mee, ma buru ụzọ hụ nsogbu site na iji.

dịka ọmụmaatụ, ngwaọrụ ndị a nyochara na Nkebi 3 - Nyochaa Akụkọ Ọrụ Sistemụ Iji Linux Toolsets nke usoro a. Ya mere, ọ bụ ezie na o nwere ike ọ gaghị adị ka ọ na-eme ọtụtụ ihe, ọ bụ n'ihi na a na-elekọta ọtụtụ ọrụ ya site n'enyemaka nke shei scripting, bụ ihe anyị ga-ekwu maka ya na nkuzi a.

Gịnị bụ a shei script?

N'okwu ole na ole, script shei abụghị ihe ọzọ ma ọ bụ ihe na-erughị mmemme nke a na-eme nzọụkwụ site na shei, nke bụ ihe omume ọzọ na-enye ihe ntanetị n'etiti Linux kernel na onye ọrụ njedebe.

Site na ndabara, shei a na-eji maka akaụntụ onye ọrụ na RHEL 7 bụ bash (/bin/bash). Ọ bụrụ na ịchọrọ nkọwa zuru ezu na ụfọdụ ndabere akụkọ ihe mere eme, ị nwere ike ịtu aka na edemede Wikipedia a.

Iji chọpụta ihe ndị ọzọ gbasara nnukwu atụmatụ nke shei a nyere, ị nwere ike ịchọ ịlele ibe nwoke ya, nke ebudatara na PDF na (A Guide from Newbies to SysAdmin article na linux-console.net tupu ịga n'ihu). Ugbu a, ka anyị malite.

Ide edemede iji gosi ozi sistemu

Maka ịdị mma anyị, ka anyị mepụta ndekọ iji chekwaa scripts shei anyị:

# mkdir scripts
# cd scripts

Mepee faịlụ ederede ọhụrụ aha ya bụ system_info.sh yana onye ndezi ederede masịrị gị. Anyị ga-amalite site n'itinye okwu ole na ole n'elu yana ụfọdụ iwu emechaa:

#!/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"

Na-esote, nye script mebie ikike:

# chmod +x system_info.sh

ma gbaa ya:

./system_info.sh

Rịba ama na a na-egosi nkụnye eji isi mee nke ngalaba ọ bụla na agba maka nhụhụta ka mma:

Enyere ọrụ ahụ site na iwu a:

echo -e "\e[COLOR1;COLOR2m<YOUR TEXT HERE>\e[0m"

Ebe COLOR1 na COLOR2 bụ ihu ihu na agba agba, n'otu n'otu (a na-akọwa nkọwa ndị ọzọ na nhọrọ na ntinye a sitere na Arch Linux Wiki) na <Ụdị gị Ebe a> bụ eriri nke ịchọrọ igosi na agba.

Ọrụ akpaaka

Ọrụ ndị ị nwere ike ịchọrọ iji rụọ ọrụ nwere ike ịdịgasị iche site na ikpe gaa na ikpe. Ya mere, anyị enweghị ike ikpuchi ọnọdụ niile enwere ike n'otu edemede, mana anyị ga-ewepụta ọrụ atọ kpochapụrụ nke enwere ike ịmegharị ya site na iji edemede shei:

1) melite nchekwa data faịlụ mpaghara, 2) chọta (ma hichapụ ọzọ) faịlụ nwere ikike 777, yana 3) mara mgbe ojiji sistemụ faịlụ gafere oke akọwapụtara.

Ka anyị mepụta faịlụ aha ya bụ auto_tasks.sh na ndekọ ederede anyị nwere ọdịnaya ndị a:

#!/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
done < <(df -h --total | grep -vi filesystem)

Biko mara na enwere oghere n'etiti akara < abụọ n'ahịrị ikpeazụ nke edemede ahụ.

Iji Cron

Iji mee nke ọma otu nzọụkwụ n'ihu, ị gaghị achọ ịnọdụ n'ihu kọmputa gị wee jiri aka mee ihe odide ndị ahụ. Kama, ị ga-eji cron hazie ọrụ ndị ahụ ka ha na-agba ọsọ kwa oge wee ziga nsonaazụ ya na ndepụta ndị nnata eburu ụzọ site na email ma ọ bụ chekwaa ha na faịlụ nke enwere ike ịlele site na iji ihe nchọgharị weebụ.

Edemede na-esonụ (filesystem_usage.sh) ga-eme iwu df -h a ma ama, hazie mmepụta ya na tebụl HTML wee chekwaa ya na faịlụ 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

Na sava RHEL 7 anyị (192.168.0.18), nke a dị ka ndị a:

Ị nwere ike ịgbakwunye na akụkọ ahụ ruo ọtụtụ ozi dịka ịchọrọ. Iji mee edemede ahụ kwa ụbọchị na 1:30 ehihie, tinye ntinye crontab na-esonụ:

30 13 * * * /root/scripts/filesystem_usage.sh

Nchịkọta

O yikarịrị ka ị ga-eche maka ọtụtụ ọrụ ndị ọzọ ịchọrọ ma ọ bụ mkpa ịmegharị; dịka ị na-ahụ, iji ederede shei ga-eme ka mbọ a dị mfe. Enwere onwe gị ime ka anyị mara ma ọ bụrụ na ịchọta isiokwu a na-enyere aka ma egbula oge itinye echiche ma ọ bụ nkwupụta gị site na ụdị dị n'okpuru.