nagios 監控tcp連接數 物理內存 cpu使用率 web併發數 mysql主從狀態

1、centos6.2 操作系統 監控本機tcp連接數 物理內存 cpu使用率 web併發數

查看本機tcp連接狀態

 netstat -n |awk '/^tcp/ {++s[$NF]} END {for (a in s) print a,s[a]}'

LISTEN:偵聽來自遠方的TCP端口的連接請求
SYN-SENT:再發送連接請求後等待匹配的連接請求
SYN-RECEIVED:再收到和發送一個連接請求後等待對方對連接請求的確認
ESTABLISHED:代表一個打開的連接
FIN-WAIT-1:等待遠程TCP連接中斷請求,或先前的連接中斷請求的確認
FIN-WAIT-2:從遠程TCP等待連接中斷請求
CLOSE-WAIT:等待從本地用戶發來的連接中斷請求
CLOSING:等待遠程TCP對連接中斷的確認
LAST-ACK:等待原來的發向遠程TCP的連接中斷請求的確認
TIME-WAIT:等待足夠的時間以確保遠程TCP接收到連接中斷請求的確認
CLOSED:沒有任何連接狀態
在nagios的插件庫裏面寫下腳本

寫腳本ip_cons
#!/bin/bash

#tcp connet
if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
ip_conns=`netstat -n |grep ESTABLISHED|wc -l`
if [ $ip_conns -lt $1 ];then
echo "OK -connet counts is $ip_conns"
exit 0
fi
if [ $ip_conns -ge $1 -a  $ip_conns -lt $2 ];then
echo "Warning -connet counts is $ip_conns"
exit 1
fi
if [ $ip_conns -ge $2 ];then
echo "Critical -connet counts is $ip_conns"
exit 2
fi

修改配置文件commands.cfg

commands.cfg添加命令
define command{
command_name ip_cons
command_line /usr/local/nagios/libexec/ip_cons 2 5 (2 是警告值 5是臨界值可自定義這裏只是做實驗)
 }

之後修改本機的配置文件localhost.cfg

define service{
        use                             generic-service
        host_name                       localhost
        service_description             ip_cons
        check_command                   ip_cons
        notifications_enabled           1
        }

或者是寫一個services.cfg文件裏裏面

define service{
        host_name  localhost
        service_description ip_cons
        check_command ip_cons
        max_check_attempts 5
        normal_check_interval 3
        retry_check_interval 2
        check_period 24x7
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        contact_groups admins
}

寫入services.cfg文件之後需要在nagios.cfg裏面添加

cfg_file=/etc/nagios/objects/services.cfg

之後查看配置文件是否出錯 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

沒有出錯的話  就重新啓動nagios服務 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

2、centos6.2 操作系統 監控其他主機tcp連接數 物理內存 cpu使用率 web併發數

寫監控腳本
#!/bin/bash
if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
ip_conns=`netstat -n |grep ESTABLISHED|wc -l`
if [ $ip_conns -lt $1 ];then
echo "OK -connet counts is $ip_conns"
exit 0
fi
if [ $ip_conns -ge $1 -a  $ip_conns -lt $2 ];then
echo "Warning -connet counts is $ip_conns"
exit 1
fi
if [ $ip_conns -ge $2 ];then
echo "Critical -connet counts is $ip_conns"
exit 2
fi
在nrpe.cfg 裏面填寫監控命令
command[ip_cons]=/usr/local/nagios/libexec/ip_cons 5 9
之後在監控機器上的配置文件裏面配置
define service{
       use                             generic-service
        host_name                       192.168.122.3
        service_description             ip_cons
        check_command                   check_nrpe!ip_cons
        notifications_enabled           1
        }

或者是寫入services.cfg文件

define service{
        host_name  192.168.122.3
        service_description ip_cons
        check_command check_nrpe!ip_cons
        max_check_attempts 5
        normal_check_interval 3
        retry_check_interval 2
        check_period 24x7
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        contact_groups admins
}

之後重新啓動nagios服務即可

內存監控的做法基本上和以上做法相同,想來要做這個監控的都是運維的同行,應該可以理解下面就只留下內存的監控腳本

#!/bin/bash
#memory 

if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
total_mem=`free -m |grep Mem|awk '{print $2}'`
free_mem=`free -m |grep Mem|awk '{print $4}'`
used_mem=`free -m |grep Mem|awk '{print $3}'`
if [ $free_mem -gt $1 ];then
echo "OK - total memory  $total_mem MB used  $used_mem MB free $free_mem MB "
exit 0
fi
if [ $free_mem -ge $2 -a $free_mem -le $1 ];then
echo "Warning - total memory  $total_mem MB used $used_mem MB free $free_mem MB"
exit 1
fi
if [ $free_mem -lt $2 ];then
echo "Critical - total memory  $total_mem MB  used $used_mem MB free $free_mem MB"
exit 2
fi

cpu使用率的監控跟上面tcp監控方法類似 留下監控腳本一份

#!/bin/bash
if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
used_cpu=`vmstat |sed -n 3p|awk -F " " '{print $13}'`
if [ $used_cpu -lt $1 ];then
echo "OK -used_cpu is $used_cpu %"
exit 0
fi
if [ $used_cpu -ge $1 -a  $used_cpu -lt $2 ];then
echo "Warning - used_cpu is $used_cpu %"
exit 1
fi
if [ $used_cpu -ge $2 ];then
echo "Critical - used_cpu is $used_cpu %"
exit 2
fi

 

此shell腳本中這條命令used_cpu=`vmstat |sed -n 3p|awk -F " " '{print $13}'`是監控系統使用的cpu輸出默認是整數 要是想監控剩餘cpu的話將以上shell腳本的此條命令修改成used_cpu=`vmstat |sed -n 3p|awk -F " " '{print $15}'`

web併發數 如果是apache做的站點的話 倒是可以直接監控httpd的進程數就可以獲得併發數 nginx的話就不行了 可以使用一下腳本

web併發數
#!/bin/bash
#if [ $# != 2 ];then
#echo "Usage:$0 -w num1 -c num2"
#exit
#fi
#w=2
#c=5
ip_conns=`netstat -n |grep '^tcp'|grep ESTABLISHED|grep 80|wc -l`
if [ $ip_conns -lt $1 ];then
echo "OK -connet counts is $ip_conns"
exit 0
fi
if [ $ip_conns -ge $1 -a  $ip_conns -lt $2 ];then
echo "Warning -connet counts is $ip_conns"
exit 1
fi
if [ $ip_conns -ge $2 ];then
echo "Critical -connet counts is $ip_conns"
exit 2
fi

 

監控mysql主從狀態
#!/bin/bash
io=`/usr/local/mysql/bin/mysql -u root -pXXXX -e "show slave status\G" |grep Slave_IO_Running|awk '{print $2}'`
sql=`/usr/local/mysql/bin/mysql -u root -pXXXX -e "show slave status\G" |grep Slave_SQL_Running|awk '{print $2}'`
if [[ $io = Yes && $sql = Yes ]]; then
echo "OK - mysql-replication is running"
exit 0
fi
if [[ $io = No || $sql = No ]];then
echo "Critical - mysql-replication is not working, I/O or SQL is not working properly "
exit 2
fi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章