1.1 If statement
(1) If conditional statement syntax: Single branch structure
if [condition]
Then
Instructions
Fi
Or
if [Condition];then
Instructions
Fi
If single branch condition Chinese programming Image grammar
If [you have a room]
So
I'll marry you.
Fruit such as
The previous file condition expression [-F ' $file 1 "]&& Echo 1 is equivalent to the following if statement
If [-F ' $file 1 "];then
Echo 1
Fi
(2) Dual-branch structure
Grammar:
if [condition]
Then
Instruction Set 1
Else
Instruction Set 2
Fi
The above is equivalent to the file condition expression [-F ' $file 1 "]&&echo 1| | Echo 0
If dual branch Chinese programming syntax image description
If [you have a room]
So
I'll marry you.
Otherwise
Goodbye
Fruit such as
(3) Multi-branch structure
Grammar:
if [condition 1]
Then
Directive 1
elif [Condition 2]
Then
Directive 2
Else
Directive 3
Fi
------------------------multiple Elif--------------------------
if [condition 1]
Then
Directive 1
elif [Condition 2]
Then
Directive 2
elif [Condition 3]
Then
Directive 3
.............
Else
Directive 4
Fi
Multi-Branch If statement Chinese programming grammar image metaphor
If [you have a room] <== rich
So
I'll marry you.
Or if [your father is Li Gang] <== right
So
I'll marry you.
Or if [you're trying very hard to endure]<== have the potential
So
We can start with the object
Otherwise
No, bird, you <== be eliminated.
Fruit such as
1.2 Example
Monitoring system memory and alerting Enterprise case script development combat
Problem: Develop shell script to determine the size of the system reputation memory, if less than 100M mail alarm to the administrator, and join the system scheduled tasks are executed every 3 minutes.
Solution: Focus on the problem solving process, the first step, the second part, the third part
Actual operation:
(1) Remove the command-line conditions first
[[email protected] ~]# free -m total used free sharedbuffers cachedMem: 981226754 0 48 57-/+ buffers/cache:120860 Swap: 1983 0 1983 [[email protected] ~]# free -m|grep buffers\/-/+ buffers/cache:120860 [[email protected] ~]# free -m|grep buffers\/|awk ‘ {print $NF}‘860[[email protected] ~]# free -m|grep buffers/|awk ‘{print $NF}‘854[[email protected] ~]# free -m|awk ‘NR==3 {print $NF}‘854
(2) scripting
#做一个定义cur_free=’free –m|awk ‘/buffers\// {print $NF}’’chars=”current memory is $cur_free.”if [ $cur_free –lt 100 ] Then Echo $chars|mail –s “$chars” [email protected]fi
1.3 Expansion; Monitoring disks, NFS systems, mysql,web<--monitoring resources
(1) Take the monitoring disk as an example
Read the command line first and then determine if the disk usage is below the set value if it is lower than the email alert.
[[email protected] ~]# df -hfilesystemSize Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 18G 816M 16G 5% /tmpfs 491M 0 491M 0% /dev/shm/dev/sda1 477M 33M 419M 8% /boot[[email protected] ~]# df -h|awk ‘NR==3 {print $3}‘16G
(2) monitor the MySQL service to see if the port does not exist
Note: Do not remove the port value, wc–l statistics on the end, the problem is simplified.
[[email protected] ~]# netstat -lnt|grep 3306tcp0 0 0.0.0.0:33060.0.0.0:* LISTEN [[email protected] ~]# netstat -lnt|grep 3306|wc -l1
1.4 Combat:
Using the If two-branch implementation of Apache or MySQL service is normal judgment, the use of process number, port, URL of one way, if the process does not start the process started.
(1) Use port to judge MySQL
[[email protected] ~]# cat mysql.sh #!/bin/basha=$(netstat -lntup|grep mysql|wc -l)echo $a if [ "$a" -eq "1" ];then echo "mysql is start" else echo "mysql is stop.starting " /etc/init.d/mysqld start Fi
(2) Use port to judge Apache
[[email protected] ~]# cat apache.sh #!/bin/bashapache=$(netstat -lntup|grep httpd|wc -l)echo $apacheif [ "$apache" -eq "1" ];then echo "apache is starting..." else echo “starting is not starting...” /usr/local/apache/bin/apachectl startFi
1.5 compares the size of two numbers in read-in mode. Please use the Read method to implement.
[[email protected] ~]# cat read3.sh #!?bin/bashread -p "please input nun1 num2:" a bif [ "$a" -eq "$b" ];then echo "$a 等于 $b"elif [ "$a" -gt "$b" ];then echo "$a 大于 $b"elif [ "$a" -lt "$b" ];then echo "$a 小于 $b" fi
1.6 After-school homework
Monitor Web services for normal, no less than 5 ideas
Monitor MySQL service is normal, no less than 5 in the idea.
Method: Web Service and Database (Mysql) Common approach
(1) port
Local: Netstat/ss/lsof
Remote: TELNET/NMAP/NC not on a single machine
See if the remote port is unobstructed 3 simple example cases!
http://oldboy.blog.51cto.com/2561410/942530
(2) process (local) Ps–ef|grep mysql|wc-l
(3) Wget/curl (HTTP method, judge data return value or return content)
(4) header (HTTP), (HTTP mode, judging by status code)
(5) database-specific connection via MySQL client connection, depending on the return value or return content.
1.6.1 monitoring MySQL Service 1.6.1.1 Local
(1) Netstat–lnup|grep 3306|wc–l
Note that this port must be unique, not the only number of MySQL ports on the system that are written
(2) Netstat-lnt|grep 3306|awk-f "[:]+" ' {print $} ' value comparison equals 3306
(3) Netstat–lntup|grep mysqld|wc–l
(4) [" netstat -lnt|grep 3306|awk -F "[ :]+" ‘{print $5}‘
" = "3306"]
This is to take out the MySQL process and 3306 comparison if 3306 means that the MySQL service start is not equal to the service does not start, this method is the most stupid should not be taken out to compare the number should be calculated directly with Wc–l. Example: Netstat-lntup|grep mysqld|wc–l
(5) [ ps -ef|grep mysql|grep -v grep|wc -l
-GT 0]
The grep–v I understand here is the idea of anti-election, because grep MySQL also produces a process with the MySQL keyword.
To view the MySQL process if it is a multi-instance, do not grep MySQL the name of the port where grep is the only value.
(6) Ss-lntup|grep 3306|wc–l
(7) Lsof-i: 3306|grep mysql|wc–l
No lsof command to install Yum
1.6.1.2 Remote View Port
Viewing remote ports generally rarely using Telnet, it is recommended to use NMAP to view the remote port's open status to see if the remote port is open. If no namp is installed with Yum. Port Open service is not necessarily normal, port does not open service must not be normal. So when the number of servers is more general will judge the port. In the production environment, more than Nmap is used.
(1) nmap 192.168.1.113-p 3306 2>/dev/null|grep open|wc–l
(2) echo-e "\ n" |telnet 192.168.1.113 3306|grep connected|wc-l
(3) Nc-v-W 2 192.168.1.113-z 3306 2>/dev/null |grep succeeded|wc–l
We are executing nc-v-W 2 192.168.1.113-z 3306 This command is likely to cause such an error f?jhost ' 192.168.1.108 ' is not allowed to connect to this MySQL server 。 This error occurs because remote access to MySQL is not allowed, so we want to create a Telnet user and authorize it.
1.6.1.3 taking MySQL as an example comprehensive explanation via local and remote view ports
Determine service start-up by local and remote view ports if the service is not restarted, the service is started. Altogether seven methods, also involves a keyless entry remote host very simple here I don't explain, like.
1.6.2 Monitoring Web Services
MySQL views the local and remote port methods, and the Web service also applies. This is not explained in detail, just talk about curl and wget two ways. Specific methods such as.
Five ways to monitor Web services with Curl
How to monitor Web services for wget
1.6.3 Summary
(1) port
Local: Ss,netstat,lsof
Remote: TELNET,NMAP,NC
Echo-e "\ n" |telnet 192.168.1.113 3306|grep connected|wc–l
Nmap 192.168.1.113-p 3306 2>/dev/null|grep open|wc–l
Nc-v-W 2 192.168.1.113-z 3306 2>/dev/null |grep succeeded|wc–l
See if the remote port is unobstructed 3 simple example cases!
http://oldboy.blog.51cto.com/2561410/942530
(2) Local port
(3) header (HTTP code) curl–i Web address return 200 OK
Curl-i-M 10-o/dev/null-s-W "%{http_code}\n" http://192.168.1.113
Mastering technical ideas is more important than solving the problem itself
http://oldboy.blog.51cto.com/2561410/1196298
(4) URL (wget,curl) Web address, how to impersonate a user
(5) Php.java write a program that simulates the way the user monitors.
Shell Script Programming Learning notes-branching and looping structures