├── README.md └── mon ├── bin └── main.sh ├── conf ├── mon.conf └── mon.conf~ ├── log ├── err.log ├── load.tmp └── mon.log ├── mail ├── mail.php └── mail.sh └── shares ├── 1.txt ├── 2.txt ├── 502.sh ├── 502_bak.sh ├── load.sh └── mysql.sh /README.md: -------------------------------------------------------------------------------- 1 | # monitor 2 | shell脚本监控平台 3 | 无需服务端,每台机器都要安装一下。可以自定义具体的监控项目 4 | 5 | ## author: aming 6 | ## version: 0.1 7 | 8 | 需求: 使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。 9 | 10 | 思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。 11 | 主程序:作为整个脚本的入口,是整个系统的命脉。 12 | 配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。 13 | 子程序:这个才是真正的监控脚本,用来监控各个指标。 14 | 邮件引擎:是由一个php程序来实现,它可以定义发邮件的服务器、发邮件人以及收邮件人。 15 | 输出日志:整个监控系统要有日志输出。 16 | 17 | 要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件。 18 | 19 | 程序架构: 20 | 21 | (主目录 mon) 22 | _________________________________|_____________________________________________________________ 23 | | | | | | 24 | bin conf shares mail log 25 | | | | | | 26 | [main.sh] [ mon.conf] [load.sh 502.sh] [mail.php mail.sh] [ mon.log err.log ] 27 | 28 | bin下是主程序 29 | conf下是配置文件 30 | shares下是各个监控脚本 31 | mail下是邮件引擎 32 | log下是日志。 33 | 34 | 使用:需要添加一条cron * * * * * cd /usr/local/sbin/mon/bin; bash main.sh 35 | -------------------------------------------------------------------------------- /mon/bin/main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #Written by aming. 3 | 4 | # 是否发送邮件的开关 5 | export send=1 6 | # 过滤ip地址 7 | addr=`/sbin/ifconfig |grep -A1 'eth0' |grep addr: |awk '{print $2}'|awk -F: '{print $2}'|head -1` 8 | export addr=`hostname`:$addr 9 | 10 | dir=`pwd` 11 | # 只需要最后一级目录名 12 | last_dir=`echo $dir|awk -F'/' '{print $NF}'` 13 | 14 | # 下面的判断目的是,保证执行脚本的时候,我们在bin目录里,不然监控脚本、邮件和日志很有可能找不到 15 | if [ $last_dir == "bin" ] || [ $last_dir == "bin/" ]; then 16 | conf_file="../conf/mon.conf" 17 | else 18 | echo "you shoud cd bin dir" 19 | exit 20 | fi 21 | exec 1>>../log/mon.log 2>>../log/err.log 22 | 23 | echo "`date +"%F %T"` load average" 24 | /bin/bash ../shares/load.sh 25 | 26 | #先检查配置文件中是否需要监控502 27 | if grep -q 'to_mon_502=1' $conf_file; then 28 | export log=`grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'` 29 | /bin/bash ../shares/502.sh 30 | fi 31 | 32 | -------------------------------------------------------------------------------- /mon/conf/mon.conf: -------------------------------------------------------------------------------- 1 | ## to config the options if to monitor 2 | 3 | ## cdb 主要定义mysql的服务器地址、端口以及user、password 4 | to_mon_cdb=0 ##0 or 1, default 0,0 not monitor, 1 monitor 5 | cdb_ip=192.168.11.113 6 | cdb_port=3308 7 | cdb_user=username 8 | cdb_pass=passwd 9 | 10 | ## httpd 如果是1则监控,为0不监控 11 | to_mon_httpd=0 12 | 13 | ## php 如果是1则监控,为0不监控 14 | to_mon_php_socket=0 15 | 16 | ## http_code_502 需要定义访问日志的路径 17 | to_mon_502=0 18 | logfile=/data/log/xxx.xxx.com/access.log 19 | 20 | ## request_count 定义日志路径以及域名 21 | to_mon_request_count=0 22 | req_log=/data/log/www.lishiming.net/access.log 23 | domainname=www.lishiming.net 24 | -------------------------------------------------------------------------------- /mon/conf/mon.conf~: -------------------------------------------------------------------------------- 1 | ## to config the options if to monitor 2 | 3 | ## cdb 主要定义mysql的服务器地址、端口以及user、password 4 | to_mon_cdb=0 ##0 or 1, default 0,0 not monitor, 1 monitor 5 | cdb_ip=10.20.3.13 6 | cdb_port=3315 7 | cdb_user=username 8 | cdb_pass=passwd 9 | 10 | ## httpd 如果是1则监控,为0不监控 11 | to_mon_httpd=0 12 | 13 | ## php 如果是1则监控,为0不监控 14 | to_mon_php_socket=0 15 | 16 | ## http_code_502 需要定义访问日志的路径 17 | to_mon_502=1 18 | logfile=/data/log/xxx.xxx.com/access.log 19 | 20 | ## request_count 定义日志路径以及域名 21 | to_mon_request_count=0 22 | req_log=/data/log/www.discuz.net/access.log 23 | domainname=www.discuz.net 24 | -------------------------------------------------------------------------------- /mon/log/err.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminglinux/monitor/0a1401b58a5b68c02c3a42832bddf614bf6ecb41/mon/log/err.log -------------------------------------------------------------------------------- /mon/log/load.tmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminglinux/monitor/0a1401b58a5b68c02c3a42832bddf614bf6ecb41/mon/log/load.tmp -------------------------------------------------------------------------------- /mon/log/mon.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminglinux/monitor/0a1401b58a5b68c02c3a42832bddf614bf6ecb41/mon/log/mon.log -------------------------------------------------------------------------------- /mon/mail/mail.php: -------------------------------------------------------------------------------- 1 | debug = FALSE; 20 | $this->smtp_port = $smtp_port; 21 | $this->relay_host = $relay_host; 22 | $this->time_out = 30; //is used in fsockopen() 23 | # 24 | $this->auth = $auth;//auth 25 | $this->user = $user; 26 | $this->pass = $pass; 27 | # 28 | $this->host_name = "localhost"; //is used in HELO command 29 | $this->log_file = ""; 30 | $this->sock = FALSE; 31 | } 32 | /* Main Function */ 33 | function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 34 | { 35 | $mail_from = $this->get_address($this->strip_comment($from)); 36 | $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); 37 | $header = "MIME-Version:1.0\r\n"; 38 | if($mailtype=="HTML"){ 39 | $header .= "Content-Type:text/html\r\n"; 40 | } 41 | $header .= "To: ".$to."\r\n"; 42 | if ($cc != "") { 43 | $header .= "Cc: ".$cc."\r\n"; 44 | } 45 | $header .= "From: $from<".$from.">\r\n"; 46 | $header .= "Subject: ".$subject."\r\n"; 47 | $header .= $additional_headers; 48 | $header .= "Date: ".date("r")."\r\n"; 49 | $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 50 | list($msec, $sec) = explode(" ", microtime()); 51 | $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 52 | $TO = explode(",", $this->strip_comment($to)); 53 | if ($cc != "") { 54 | $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 55 | } 56 | 57 | if ($bcc != "") { 58 | $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 59 | } 60 | $sent = TRUE; 61 | foreach ($TO as $rcpt_to) { 62 | $rcpt_to = $this->get_address($rcpt_to); 63 | 64 | if (!$this->smtp_sockopen($rcpt_to)) { 65 | $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 66 | $sent = FALSE; 67 | continue; 68 | } 69 | if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 70 | $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 71 | } else { 72 | $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 73 | $sent = FALSE; 74 | } 75 | fclose($this->sock); 76 | $this->log_write("Disconnected from remote host\n"); 77 | } 78 | return $sent; 79 | } 80 | /* Private Functions */ 81 | function smtp_send($helo, $from, $to, $header, $body = "") 82 | { 83 | if (!$this->smtp_putcmd("HELO", $helo)) { 84 | return $this->smtp_error("sending HELO command"); 85 | } 86 | #auth 87 | if($this->auth){ 88 | if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 89 | return $this->smtp_error("sending HELO command"); 90 | } 91 | if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 92 | return $this->smtp_error("sending HELO command"); 93 | } 94 | } 95 | # 96 | if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 97 | return $this->smtp_error("sending MAIL FROM command"); 98 | } 99 | if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 100 | return $this->smtp_error("sending RCPT TO command"); 101 | } 102 | if (!$this->smtp_putcmd("DATA")) { 103 | return $this->smtp_error("sending DATA command"); 104 | } 105 | if (!$this->smtp_message($header, $body)) { 106 | return $this->smtp_error("sending message"); 107 | } 108 | if (!$this->smtp_eom()) { 109 | return $this->smtp_error("sending . [EOM]"); 110 | } 111 | if (!$this->smtp_putcmd("QUIT")) { 112 | return $this->smtp_error("sending QUIT command"); 113 | } 114 | return TRUE; 115 | } 116 | 117 | function smtp_sockopen($address) 118 | { 119 | if ($this->relay_host == "") { 120 | return $this->smtp_sockopen_mx($address); 121 | } else { 122 | return $this->smtp_sockopen_relay(); 123 | } 124 | } 125 | function smtp_sockopen_relay() 126 | { 127 | $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 128 | $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 129 | if (!($this->sock && $this->smtp_ok())) { 130 | $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 131 | $this->log_write("Error: ".$errstr." (".$errno.")\n"); 132 | return FALSE; 133 | } 134 | $this->log_write("Connected to relay host ".$this->relay_host."\n"); 135 | return TRUE; 136 | } 137 | function smtp_sockopen_mx($address) 138 | { 139 | $domain = ereg_replace("^.+@([^@]+)$", "\1", $address); 140 | if (!@getmxrr($domain, $MXHOSTS)) { 141 | $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 142 | return FALSE; 143 | } 144 | foreach ($MXHOSTS as $host) { 145 | $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 146 | $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 147 | if (!($this->sock && $this->smtp_ok())) { 148 | $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 149 | $this->log_write("Error: ".$errstr." (".$errno.")\n"); 150 | continue; 151 | } 152 | $this->log_write("Connected to mx host ".$host."\n"); 153 | return TRUE; 154 | } 155 | $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 156 | return FALSE; 157 | } 158 | function smtp_message($header, $body) 159 | { 160 | fputs($this->sock, $header."\r\n".$body); 161 | $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 162 | return TRUE; 163 | } 164 | function smtp_eom() 165 | { 166 | fputs($this->sock, "\r\n.\r\n"); 167 | $this->smtp_debug(". [EOM]\n"); 168 | return $this->smtp_ok(); 169 | } 170 | function smtp_ok() 171 | { 172 | $response = str_replace("\r\n", "", fgets($this->sock, 512)); 173 | $this->smtp_debug($response."\n"); 174 | if (!ereg("^[23]", $response)) { 175 | fputs($this->sock, "QUIT\r\n"); 176 | fgets($this->sock, 512); 177 | $this->log_write("Error: Remote host returned \"".$response."\"\n"); 178 | return FALSE; 179 | } 180 | return TRUE; 181 | } 182 | function smtp_putcmd($cmd, $arg = "") 183 | { 184 | if ($arg != "") { 185 | if($cmd=="") $cmd = $arg; 186 | else $cmd = $cmd." ".$arg; 187 | } 188 | fputs($this->sock, $cmd."\r\n"); 189 | $this->smtp_debug("> ".$cmd."\n"); 190 | return $this->smtp_ok(); 191 | } 192 | function smtp_error($string) 193 | { 194 | $this->log_write("Error: Error occurred while ".$string.".\n"); 195 | return FALSE; 196 | } 197 | function log_write($message) 198 | { 199 | $this->smtp_debug($message); 200 | if ($this->log_file == "") { 201 | return TRUE; 202 | } 203 | $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 204 | if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 205 | $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 206 | return FALSE;; 207 | } 208 | flock($fp, LOCK_EX); 209 | fputs($fp, $message); 210 | fclose($fp); 211 | return TRUE; 212 | } 213 | function strip_comment($address) 214 | { 215 | $comment = "\([^()]*\)"; 216 | while (ereg($comment, $address)) { 217 | $address = ereg_replace($comment, "", $address); 218 | } 219 | return $address; 220 | } 221 | function get_address($address) 222 | { 223 | $address = ereg_replace("([ \t\r\n])+", "", $address); 224 | $address = ereg_replace("^.*<(.+)>.*$", "\1", $address); 225 | return $address; 226 | } 227 | function smtp_debug($message) 228 | { 229 | if ($this->debug) { 230 | echo $message; 231 | } 232 | } 233 | } 234 | $file = $argv[2]; 235 | $smtpserver = "smtp.qq.com";//SMTP服务器 236 | $smtpserverport = "25";//SMTP服务器端口 237 | $smtpusermail = "xxxxxxxx@qq.com";//SMTP服务器的用户邮箱 238 | //$smtpemailto = "xxxxxx@qq.com";//发送给谁 239 | $smtpemailto = "xxxxxx@139.com";//发送给谁 240 | $smtpuser = "xxxxx";//SMTP服务器的用户帐号 241 | $smtppass = "xxxxxx";//SMTP服务器的用户密码 242 | $mailsubject = $argv[1];//邮件主题 243 | $mailbody = file_get_contents($file);//邮件内容 244 | $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 245 | $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 246 | //$smtp->debug = TRUE;//是否显示发送的调试信息 247 | $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 248 | ?> 249 | -------------------------------------------------------------------------------- /mon/mail/mail.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | log=$1 4 | t_s=`date +%s` 5 | t_s2=`date -d "2 hours ago" +%s` 6 | if [ ! -f /tmp/$log ] 7 | then 8 | echo $t_s2 > /tmp/$log 9 | fi 10 | t_s2=`tail -1 /tmp/$log|awk '{print $1}'` 11 | echo $t_s>>/tmp/$log 12 | v=$[$t_s-$t_s2] 13 | echo $v 14 | if [ $v -gt 3600 ] 15 | then 16 | /usr/local/php/bin/php ../mail/mail.php "$1 $2" "$3" 17 | echo "0" > /tmp/$log.txt 18 | else 19 | if [ ! -f /tmp/$log.txt ] 20 | then 21 | echo "0" > /tmp/$log.txt 22 | fi 23 | nu=`cat /tmp/$log.txt` 24 | nu2=$[$nu+1] 25 | echo $nu2>/tmp/$log.txt 26 | if [ $nu2 -gt 10 ] 27 | then 28 | /usr/local/php/bin/php ../mail/mail.php "trouble continue 10 min $1 $2 " "$3" 29 | echo "0" > /tmp/$log.txt 30 | fi 31 | fi 32 | 33 | -------------------------------------------------------------------------------- /mon/shares/1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminglinux/monitor/0a1401b58a5b68c02c3a42832bddf614bf6ecb41/mon/shares/1.txt -------------------------------------------------------------------------------- /mon/shares/2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminglinux/monitor/0a1401b58a5b68c02c3a42832bddf614bf6ecb41/mon/shares/2.txt -------------------------------------------------------------------------------- /mon/shares/502.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | stat=`curl --connect-timeout 3 -I www.apelearn.com 2>/tmp/502.log | grep HTTP |awk '{print $2}'` 3 | ip=`dig www.apelearn.com |grep 'IN\sA' |tail -n1 |awk '{print $NF}'` 4 | if [ -z $stat ] || [ $stat -ne "200" ] 5 | then 6 | /bin/bash ../mail/mail.sh apelearn_$stat $ip /tmp/502.log 7 | fi 8 | echo "`date +%T` apelearn $stat" 9 | -------------------------------------------------------------------------------- /mon/shares/502_bak.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | stat=`curl --connect-timeout 3 -I www.apelearn.com 2>/tmp/502.log | grep HTTP |awk '{print $2}'` 3 | ip=`dig www.apelearn.com |grep 'IN\sA' |tail -n1 |awk '{print $NF}'` 4 | if [ -z $stat ] || [ $stat -ne "200" ] 5 | then 6 | /bin/bash ../mail/mail.sh apelearn_$stat $ip /tmp/502.log 7 | fi 8 | echo "`date +%T` apelearn $stat" 9 | -------------------------------------------------------------------------------- /mon/shares/load.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | ##Writen by aming## 4 | 5 | l=`uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1` 6 | load=$[$l+1] 7 | if [ $load -gt 15 ] && [ $send -eq "1" ] 8 | then 9 | echo "$addr `date +%T` load is $load" >../log/load.tmp 10 | /bin/bash ../mail/mail.sh $addr\_load $load ../log/load.tmp 11 | fi 12 | echo "`date +%T` load is $load" 13 | 14 | -------------------------------------------------------------------------------- /mon/shares/mysql.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | while : 3 | do 4 | for i in `seq 1 100`; do /usr/bin/mysql -uuch -h172.7.15.5 -P 3307 -pHNDSFnx549sJALXMNGA -e "select version()"; done 2>/tmp/3307.err |wc -l>/tmp/line.txt 5 | n=`cat /tmp/line.txt` 6 | if [ $n -lt 200 ] 7 | then 8 | date >>/tmp/mon_mysql.err 9 | /bin/bash ../mail/mail.sh db1_3307_not_connect /tmp/3307.err 10 | fi 11 | sleep 10 12 | done 13 | --------------------------------------------------------------------------------