Write SHELL scripts to monitor hosts
1. Run the vi command to create a script file sysmon. sh in the/root directory:
The content is as follows:
#! /Bin/bash
Dug = $ (df-h | grep "/$" | awk '{print $4}' | awk-F % '{print $1 }')
Cug = $ (expr 100-$ (mpstat | tail-1 | awk '{print $10}' | awk-F. '{print $1 }'))
Mug = $ (expr $ (free | grep "cache:" | awk '{print $3}') \ * 100/$ (free | grep "Mem: "| awk '{print $2 }'))
Alog = "/tmp/alert.txt"
AMAIL = "root@localhost.localdomain"
If [$ dug-gt 3]
Then
Echo "disk usage: $ dug %" >>$ alog
Fi
If [$ cug-gt 2]
Then
Echo "CPU usage: $ cug %" >>$ alog
Fi
If [$ mug-gt 2]
Then
Echo "memory usage: $ mug %" >>$ alog
Fi
If [-f $ alog]
Then
Cat $ alog | mail-s "Host Alert" $ AMAIL
Rm-fr $ alog
Fi
As follows:
650) this. width = 650; "title =" image001.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5103H7-0.png "/>
Here, the usage is a small value. In order to make the verification result more obvious, it varies according to the actual environment)
Appendix:
A: Use the df command to extract the disk usage of the root partition, as shown in:
Run the df-h command to view the usage of the disk partition. The used space of the root partition is 8%.
650) this. width = 650; "title =" image003.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q51035c-1.png "/>
Use the command in the example to filter out the space used by the disk.
[Root @ shell ~] # Df-h | grep "/$" | awk '{print $4}' | awk-F % '{print $1 }'
650) this. width = 650; "title =" image005.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5104418-2.png "/>
B: To use the mpstat command to extract CPU usage, install the sysstat package first ). As shown in: % idle indicates percentage of idle CPU time
650) this. width = 650; "title =" image007.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q510D94-3.png "/>
650) this. width = 650; "title =" image009.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5102923-4.png "/>
Use the command in the example to get the space used by the CPU and assign the value to the variable CUG.
[Root @ shell ~] # Cug = $ (expr 100-$ (mpstat | tail-1 | awk '{print $10}' | awk-F. '{print $1}') [root @ shell ~] # Echo $ cug
650) this. width = 650; "title =" image011.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5102149-5.png "/>
C: use the free command to view memory usage. The red line indicates the free memory.
650) this. width = 650; "title =" image013.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5102I0-6.png "/>
Row 3:
Total memory: 1035108
Used memory used: 500928
Free idle memory: 534180
Shared is no longer used. It is always 0.
Buffers Buffer Cache memory: 28968
Cached Page Cache memory: 348204
Link:Total = used + free
Row 3:
-/+ Buffers/cache means:
-Buffers/cache memory: 123756 (equivalent to 1st rows of used-buffers-cached)
+ Buffers/cache memory: 911352 (equal to 1st rows free + buffers + cached)
Therefore:-buffers/cache reflects the memory actually eaten by the program, while + buffers/cache reflects the total number of memories that can be used.
The third row is for swap partitions separately:
In order to improve disk access efficiency, Linux has made some careful designs, in addition to caching dentry (for VFS, accelerating the conversion of file path names to inode ), two major Cache methods are also adopted: Buffer Cache and Page Cache. The former is used to read and write disk blocks, and the latter is used to read and write inode files. These caches effectively shorten the time for I/O system calls (such as read, write, getdents.
Use the command in the example to get the memory usage and assign the value to the variable mug:
[Root @ shell ~] # Mug = $ (expr $ (free | grep "cache:" | awk '{print $3}') \ * 100/$ (free | grep "Mem: "| awk '{print $2 }'))
[Root @ shell ~] # Echo $ mug
650) this. width = 650; "title =" image015.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5105620-7.png "/>
2. Run the chmod command to add the execute x permission to the sysmon. sh file.
650) this. width = 650; "title =" image017.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q510N27-8.png "/>
3. Test the execution of the sysmon. sh script.
A: My experiment environment is Linux 5.5. Use its own sendmail to send an email for verification:
650) this. width = 650; "title =" image019.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5102131-9.png "/>
Supplement: all mail ports are port 25 and ICMP protocol]
B: Check whether an email server is available, lower the monitoring threshold, and run the sysmon. sh script for testing.
C: Check the mailbox Production Environment named root@localhost.localdomain to write the specific alarm mailbox), confirm the warning mail content
650) this. width = 650; "title =" image021.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q510D26-10.png "/>
The email content is as follows:
650) this. width = 650; "title =" image023.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5106304-11.png "/>
4. Set the crontab to schedule the task, call the sysmon. sh script once every half hour, and restart the crond service.
650) this. width = 650; "title =" image025.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q51050I-12.png "/>
650) this. width = 650; "title =" image027.png "src =" http://www.bkjia.com/uploads/allimg/131227/1Q5101231-13.png "/>
5. The operation is complete.
This article from the "heard" blog, please be sure to keep this source http://wenzhongxiang.blog.51cto.com/6370734/1213518