Configure the server to monitor the private line network Configure the server to monitor the private line network

Configuring Server Monitoring Leased Network

 

A goal is a dream with a deadline. Much effort, much prosperity.

 

Configuring Server Monitoring Leased Network

Project Introduction:

Configure a server to monitor the status of the private line network, (due to the company's business, the private line needs to be used to connect with customers) real-time monitoring of the status of the private line, and alarm if the private line fails

Alarm methods include email alarm, WeChat alarm, and monitor alarm, etc.

The email alarm built by the company is to write a script to monitor whether the network is unobstructed. If it fails, send an email to the alarm directly. It is more direct and convenient. Compared with the traditional monitor monitoring services (zabbix, nagios, cacti, etc.), it is more intuitive, (personal opinion) After all, the dedicated line network is very important! WeChat alarm is more cumbersome and more troublesome, and it needs to call the interface of WeChat or something.

Project preparation:

A server, because only one script needs to run, does not take up much resources, it can be placed on other servers (such as test servers, log servers, monitoring servers, etc. to avoid production servers as much as possible) Because the project has been deployed, I am here on a virtual machine Perform restore steps

 

1
2
3
4
[root@localhost ~] # cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@localhost ~] # uname -a
Linux localhost.localdomain 2.6.32-696.16.1.el6.x86_64  #1 SMP Wed Nov 15 16:51:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Configure the installation:

Install the mail system sendmail

 

1
2
3
4
5
6
7
8
9
[root@localhost ~] # cd /usr/src/
[root@localhost src] #
wget -c http: //caspian .dotconf.net /menu/Software/SendEmail/sendEmail-v1 .56. tar .gz
[root@localhost src] # tar xf sendEmail-v1.56.tar.gz
[root@localhost src] # cd sendEmail-v1.56
[root@localhost sendEmail-v1.56] # cp -a sendEmail /usr/local/bin/
[root@localhost sendEmail-v1.56] # chmod +x /usr/local/bin/sendEmail
[root@localhost sendEmail-v1.56] # file /usr/local/bin/sendEmail
/usr/local/bin/sendEmail : a  /usr/bin/perl  -w script text executable

Install dependencies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~] # yum -y install perl-Net-SSLeay perl-IO-Socket-SSL
[root@localhost ~] # sendEmail -f [email protected] -t [email protected] -s smtp.163.com -u "你好王叔叔" -o message-content-type=html -o message-charset=utf8 -xu [email protected] -xp *** -m "邮件主题:别来无恙"
Jan 10 05:02:30 localhost sendEmail[14290]: Email was sent successfully!
命令说明:
/usr/local/bin/sendEmail                          #命令主程序
-f  [email protected]                               #发件人邮箱
-t  [email protected]                                 #收件人邮箱
-s  smtp.huanqi.cn                                #发件人邮箱的smtp服务器
-u   "我是邮件主题"                                  #邮件的标题
-o  message-content- type =html                     #邮件内容的格式,html表示它是html格式
-o  message-charset=utf8                          #邮件内容编码
-xu  [email protected]                              #发件人邮箱的用户名
-xp  zh@123bj                                     #发件人邮箱密码
-m   "我是邮件内容"                                  #邮件的具体内容

Script monitoring Script:

The composition of the script consists of 4 files

Among them, wss.sh is the main script program, and the other three components are called by the main program

Component Description:

ip.txt is the monitored private line IP address divided into two columns, one is IP and the other is alias

mima.txt is the sender's mailbox password (IMAT/SMTP password) and can be encrypted by other programs

sou.txt is the recipient's mailbox, the alarm will send a copy to all mailboxes in this file

wss.sh is the main program, used to monitor the dedicated line, peer IP or host, call the other three files, and alarm when abnormal

script-wss.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
DATE=` date  + "%Y-%m-%d %H:%M" `
IP=路径 /ip .txt
MAIL= '发件人@163.com'
mima=` cat  . /mima .txt`
for  ip  in  $( cat  $IP |  sed  '/^#/d'  awk  '{print $1}' )
do
     name=$( cat  $IP |  grep  $ip |  awk  '{print $2}' )
     neirong=$( echo  -e  "时间:$DATE\n地址:$ip\n专线:$name\n内容:专线异常" )
     ping  -c 10 -i 0.1 $ip &> /dev/null
     if  [ $? - ne  0 ]
     then
         for  in  $( cat  . /sou .txt)
         do
             /usr/local/bin/sendEmail  -f $MAIL -t $i -s smtp.163.com -u  "$name 异常通知"  -o message-content- type =html -o message-charset=utf8 -xu 发件人@163.com -xp $mima -m  "$neirong"
         done
     fi
done

script-sou.txt

 里面写的都是收件人的邮箱

script-ip.txt

里面的第一列都是要监控的IP地址,第二列为第一列的别名

1
2
3
4
5
6
7
8
9
114.114.114.114    美国
192.168.5.1        泰国
192.168.70.70      光大
192.168.3.3        新加坡
182.168.5.222      韩国
192.168.5.222      加拿大
8.8.8.8            日本
223.5.5.5          南国
133.226.7.7        缅甸

script-mima.txt

可以用加密工具进行加密,也可以用shc进行转换,shc可以把写的所有脚本转换为c语言写的可执行文件,shc的详解后续我会再写

1
存放发件人的SMTP密码

注意

如要使用本脚本需按实际情况进行修改(路径,邮箱,smtp)!

A goal is a dream with a deadline. Much effort, much prosperity.

 

配置服务器监控专线网络

项目介绍:

配置一台服务器监控专线网络的情况,(由于公司业务的原因需要用到专线进行与客户对接)实时监控专线的状态,如果专线不通则进行报警

报警方法有邮件报警,微信报警,和监控器报警等

公司搭建的邮件报警就是写脚本进行监控网络是否通畅,不通则直接发邮件进行报警,更直接,更方便相比传统的监控器监控服务(zabbix、nagios、cacti等)更直观,(个人意见)毕竟专线的网络是挺重要的!微信报警则就显得比较繁琐更麻烦还需要调用微信的接口什么的

项目准备:

一台服务器,因为只有一个脚本需要跑占用的资源不大,可以放在其他服务器上(比如测试服务器,日志服务器,监控服务器等尽量避开生产服务器)因为项目已经部署,我这里在虚拟机上进行还原步骤

 

1
2
3
4
[root@localhost ~] # cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@localhost ~] # uname -a
Linux localhost.localdomain 2.6.32-696.16.1.el6.x86_64  #1 SMP Wed Nov 15 16:51:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

配置安装:

安装邮件系统sendmail

 

1
2
3
4
5
6
7
8
9
[root@localhost ~] # cd /usr/src/
[root@localhost src] #
wget -c http: //caspian .dotconf.net /menu/Software/SendEmail/sendEmail-v1 .56. tar .gz
[root@localhost src] # tar xf sendEmail-v1.56.tar.gz
[root@localhost src] # cd sendEmail-v1.56
[root@localhost sendEmail-v1.56] # cp -a sendEmail /usr/local/bin/
[root@localhost sendEmail-v1.56] # chmod +x /usr/local/bin/sendEmail
[root@localhost sendEmail-v1.56] # file /usr/local/bin/sendEmail
/usr/local/bin/sendEmail : a  /usr/bin/perl  -w script text executable

安装依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~] # yum -y install perl-Net-SSLeay perl-IO-Socket-SSL
[root@localhost ~] # sendEmail -f [email protected] -t [email protected] -s smtp.163.com -u "你好王叔叔" -o message-content-type=html -o message-charset=utf8 -xu [email protected] -xp *** -m "邮件主题:别来无恙"
Jan 10 05:02:30 localhost sendEmail[14290]: Email was sent successfully!
命令说明:
/usr/local/bin/sendEmail                          #命令主程序
-f  [email protected]                               #发件人邮箱
-t  [email protected]                                 #收件人邮箱
-s  smtp.huanqi.cn                                #发件人邮箱的smtp服务器
-u   "我是邮件主题"                                  #邮件的标题
-o  message-content- type =html                     #邮件内容的格式,html表示它是html格式
-o  message-charset=utf8                          #邮件内容编码
-xu  [email protected]                              #发件人邮箱的用户名
-xp  zh@123bj                                     #发件人邮箱密码
-m   "我是邮件内容"                                  #邮件的具体内容

脚本监控Script:

脚本的组成有4个文件

其中wss.sh为主脚本程序,其他三个为主程序调用的组件

组件说明:

ip.txt          为监控的专线IP地址分为两列,一列为IP,一列为别名

mima.txt        为发件人的邮箱密码(IMAT/SMTP密码)可用其他程序进行加密

sou.txt         为收件人的邮箱,报警则会向此文件里的所有邮箱发一份

wss.sh          为主程序,用于监控专线,对端IP或主机,调用其他三个文件,异常报警

script-wss.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
DATE=` date  + "%Y-%m-%d %H:%M" `
IP=路径 /ip .txt
MAIL= '发件人@163.com'
mima=` cat  . /mima .txt`
for  ip  in  $( cat  $IP |  sed  '/^#/d'  awk  '{print $1}' )
do
     name=$( cat  $IP |  grep  $ip |  awk  '{print $2}' )
     neirong=$( echo  -e  "时间:$DATE\n地址:$ip\n专线:$name\n内容:专线异常" )
     ping  -c 10 -i 0.1 $ip &> /dev/null
     if  [ $? - ne  0 ]
     then
         for  in  $( cat  . /sou .txt)
         do
             /usr/local/bin/sendEmail  -f $MAIL -t $i -s smtp.163.com -u  "$name 异常通知"  -o message-content- type =html -o message-charset=utf8 -xu 发件人@163.com -xp $mima -m  "$neirong"
         done
     fi
done

script-sou.txt

 里面写的都是收件人的邮箱

script-ip.txt

里面的第一列都是要监控的IP地址,第二列为第一列的别名

1
2
3
4
5
6
7
8
9
114.114.114.114    美国
192.168.5.1        泰国
192.168.70.70      光大
192.168.3.3        新加坡
182.168.5.222      韩国
192.168.5.222      加拿大
8.8.8.8            日本
223.5.5.5          南国
133.226.7.7        缅甸

script-mima.txt

可以用加密工具进行加密,也可以用shc进行转换,shc可以把写的所有脚本转换为c语言写的可执行文件,shc的详解后续我会再写

1
存放发件人的SMTP密码

注意

如要使用本脚本需按实际情况进行修改(路径,邮箱,smtp)!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568968&siteId=291194637