shell编程

  1 #第一题部署nginx反向代理三个web服务,调度算法使用加权轮询  4 #/bin/bash
  5 systemctl status nginx
  6 if(($?==4))
  7     then 
  8     yum install -y nginx
  9     if(($?==0))
 10         then
 11         #echo 'ok!'
 12         systemctl start nginx
 13         if(($?==0))
 14             then
 15             echo "Nginx is start!"
 16         else
 17             echo "nginx is Fail"    
 18         fi
 19     else
 20         echo""nginx  install is Fail!!!"
 21         
 22     fi 
 23 elif(($?==3))
 24     then
 25     systemctl start nginx
 26     if(($?==0))
 27         then
 28         echo "Nginx is start!"
 29     else
 30         echo "not start!!"
 31     fi
 32 elif(($?==0))
 33     then
 34     echo "alright!"
 35 else 
 36     echo "not start"    
 37 fi
 38 echo "Is writing...."
 39 grep 'upstream' /etc/nginx/nginx.conf
 40 if(($?!=0))
 41     then
 42     sed -ri '/^http/a upstream lzq {' /etc/nginx/nginx.conf
 43     sed -ri '/^upst/a server website1 weight=3\;' /etc/nginx/nginx.conf
 44     sed -ri '/^server website1 /a server website2  /a website3\;' 
 45 
 46 /etc/nginx/nginx.conf
 47     sed -ri '/^server yanlongweb2/a \}' /etc/nginx/nginx.conf
 48     sed -ri '/^server yanlongweb3/a \}' /etc/nginx/nginx.conf
 49     sed -ri '/^(\ +)(location)(\ )(\/)/a proxy_pass http:\/\/lzq\;' 
 50 
 51 /etc/nginx/nginx.conf
 52 fi
 53 echo "config write is OK!"
 54 systemctl reload nginx
 55 if(($?==0))
 56     then
 57     echo "balance is OK!"
 58 else
 59     echo "not OK!"
 60 fi
 61 systemctl status nfs
 62 if(($?==4))
 63     then 
 64     yum install rpcbind nfs-utils -y
 65     if(($?==0))
 66         then
 67         #echo 'Yes!'
 68         systemctl start nfs
 69         if(($?==0))
 70             then
 71             echo "nfs is start!"
 72         else
 73             echo "nfs is Fail!!!"    
 74         fi
 75     else
 76         echo"nfs install is Fail!!!"
 77         
 78     fi 
 79 elif(($?==3))
 80     then
 81     systemctl start nfs
 82     if(($?==0))
 83         then
 84         echo "nfs is start!"
 85     else
 86         echo "nfs not start!"
 87     fi
 88 elif(($?==0))
 89     then
 90     echo "alrgiht!"
 91 else 
 92     echo "bad"    
 93 fi
 94 echo "Is writing...."
 95 echo "/webindex 192.168.16.0/24(rw,sync,fsid=0)" > /etc/exports
 96 echo "config write is OK!"
 97 systemctl reload nfs
 98 if(($?==0))
 99     then
100     echo "NFS service is start!"
101 else
102     echo "Sorry!!"
103 fi
104 
105 #第二题所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性
106 #/bin/bash
107 systemctl status nginx
108 if(($?==4))
109         then
110         yum install -y nginx
111         if(($?==0))
112                 then
113                 #echo 'ok!'
114                 systemctl start nginx
115                 if(($?==0))
116                         then
117                         echo " Nginx is start !!"
118                 else
119                         echo "nginx is Fail!!!" 
120                 fi
121         else
122                 echo"nginx install is Fail!!!"
123 
124         fi
125 elif(($?==3))
126         then
127         systemctl start nginx
128         if(($?==0))
129                 then
130                 echo "Nginx is start!"
131         else
132                 echo "sorry!!"
133         fi
134 elif(($?==0))
135         then
136         echo "alright!"
137 else
138         echo "fail"    
139 fi
140 echo "IS writing...."
141 sed -ri '/^(\ +)(location)(\ )(\/)/a root\ \/nginxwebservice\;' 
142 
143 /etc/nginx/nginx.conf
144 sed -ri '/^root\ \/nginxwebservice/a index\ web.html\;' 
145 
146 /etc/nginx/nginx.conf
147 echo "config write is OK!"
148 systemctl reload nginx
149 if(($?==0))
150         then
151         echo "balance is OK!"
152 else
153         echo "fail!!"
154 fi
155 systemctl status nfs
156 if(($?==4))
157     then 
158     yum install rpcbind nfs-utils -y
159     if(($?==0))
160         then
161         #echo 'Yes!'
162         systemctl start nfs
163         if(($?==0))
164             then
165             echo "nfs is start OK!!"
166         else
167             echo "nfs is Fail!!!"    
168         fi
169     else
170         echo"nfs install is Fail!!!"
171         
172     fi 
173 elif(($?==3))
174     then
175     systemctl start nfs
176     if(($?==0))
177         then
178         echo " nfs is start OK!!!"
179     else
180         echo "fail!!"
181     fi
182 elif(($?==0))
183     then
184     echo "alright!"
185 else 
186     echo "bad"    
187 fi
188 echo "is writing...."
189 mount -t nfs 192.168.16.120:/website/ /nginxwebservice/
190 echo "config write is OK!"
191 systemctl reload nfs
192 if(($?==0))
193     then
194     echo "NFS  is OK!"
195 else
196     echo "bad!"
197 fi
198 
199 #第三题shell脚本-监控及邮件提醒 
200 
201 #!/usr/bin/python
202 # -*- coding: UTF-8 -*-
203 import sys
204 import smtplib
205 import email.mime.multipart
206 import email.mime.text
207 
208 server = 'smtp.163.com'
209 port = '25'
210 
211 def sendmail(server,port,user,pwd,msg):
212     smtp = smtplib.SMTP()
213     smtp.connect(server,port)
214     smtp.login(user, pwd)
215     smtp.sendmail(msg['from'], msg['to'], msg.as_string())
216     smtp.quit()
217     print('邮件发送成功email has send out !')
218 
219 
220 if __name__ == '__main__':
221     msg = email.mime.multipart.MIMEMultipart()
222     msg['Subject'] = '服务器报警请注意!'
223     msg['From'] = 'python4_mail@163.com'
224     msg['To'] = 'woshixinfode@sina.com'
225     user = 'python4_mail'
226     pwd = 'sbalex3714'
227     content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:]))
228
229 #格
式处理,专门针对我们的邮件格式 230 231 txt = email.mime.text.MIMEText(content, _charset='utf-8') 232 msg.attach(txt) 233 234 sendmail(server,port,user,pwd,msg) 235 236 #监控内容 237 #/bin/bash 238 bu=`free | awk 'NR==2{print $6}'` 239 to=`free | awk 'NR==2{print $2}'` 240 mem=`expr "scale=2;$bu/$to" |bc -l | cut -d. -f2` 241 if(($mem >= 70)) 242 then 243 msg="TIME:$(date +%F_%T) 244 HOSTNAME:$(hostname) 245 IPADDR:$(ifconfig |awk 'NR==2{print $2}') 246 MSG:内存溢出了!已经用了${mem}%" 247 echo $msg 248 /usr/bin/pymail.py $msg 249 fi 250 systemctl status nginx 251 if(($?!=0)) 252 then 253 msg="TIME:$(date +%F_%T) 254 HOSTNAME:$(hostname) 255 IPADDR:$(ifconfig |awk 'NR==2{print $2}') 256 MSG: Nginx 进程出现异常请注意查看!" 257 echo $msg 258 /usr/bin/pymail.py $msg 259 fi 260 systemctl status nfs 261 if(($?!=0)) 262 then 263 msg="TIME:$(date +%F_%T) 264 HOSTNAME:$(hostname) 265 IPADDR:$(ifconfig |awk 'NR==2{print $2}') 266 MSG: NFS 进程出现异常请注意查看!" 267 echo $msg 268 /usr/bin/pymail.py $msg 269 fi

 

posted @ 2017-03-26 21:24  woshixinfode  阅读(169)  评论(0编辑  收藏  举报