- #!/bin/bash
- #ip
- db_ip=$(/sbin/ifconfig eth0|grep "inet addr"|cut -d : -f 2|awk '{print $1}')
- #name
- db_name=$(hostname)
- monitor_name=database
- here='gansu'
- #mysql info
- mysql_ip='1.1.1.1'
- mysql_username='root'
- mysql_passwd='****'
- mysql_database='monitor'
- memory_table=''$here'_memory'
- load_table=''$here'_load'
- io_table=''$here'_io'
- hardware_table=''$here'_hardware'
- message_table=''$here'_message'
- user_table=''$here'_user'
- disk_table=''$here'_disk'
- cpu_table=''$here'_cpu'
- service_table=''$here'_service'
- #date and log
- day="$(date +%Y%m%d)"
- worklog='/usr/local/monitor/logs/all_work_log'
- downlog='/usr/local/monitor/logs/all_down_log'
- now="$(date +%Y-%m-%d-%T)"
- loghere='/usr/local/monitor/logs'
- #cpu service
- alert_cpu='80'
- db_cpu_idle="$(top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d .)"
- db_cpu="$(echo 100 - $db_cpu_idle|/usr/bin/bc)"
- #memory service
- alert_mem='100'
- db_mem="$(/usr/bin/free -m|grep Mem|awk '{print $4}')"
- db_swap="$(/usr/bin/free -m|grep Swap|awk '{print $3}')"
- alert_swap='0'
- #memory log
- memory_worklog='/usr/local/monitor/logs/mem_work_log'
- memory_downlog='/usr/local/monitor/logs/mem_down_log'
- #load service
- cpu_count="$(grep -c 'model name' /proc/cpuinfo)"
- alert_load="$(echo $cpu_count/2|/usr/bin/bc)"
- db_load="$(uptime|awk '{print $NF}'|cut -f 1 -d .)"
- db_load_15="$(uptime|awk '{print $NF}')"
- #load log
- load_worklog='/usr/local/monitor/logs/load_work_log'
- load_downlog='/usr/local/monitor/logs/load_down_log'
- #io service
- alert_io='80'
- db_io_idle_back="$(/usr/bin/iostat|awk 'NR==4{print $NF}'|cut -f 1 -d .)"
- db_io_idle="$(echo 100 - $db_io_idle_back|/usr/bin/bc)"
- #io log
- io_worklog='/usr/local/monitor/logs/io_work_log'
- io_downlog='/usr/local/monitor/logs/io_down_log'
- #hardware service
- db_hardware_error="$(dmesg|grep -i error|wc -l)"
- db_info_error="$(dmesg|grep -i error)"
- #hardware error log
- hard_worklog='/usr/local/monitor/logs/hard_work_log'
- hard_downlog='/usr/local/monitor/logs/hard_down_log'
- #message service
- db_message_error_count="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error|wc -l)"
- db_message_error="$(awk '/"$(date +%Y%m%d)"/' /var/log/messages|grep -i error)"
- #message error log
- message_worklog='/usr/local/monitor/logs/message_work_log'
- message_downlog='/usr/local/monitor/logs/message_down_log'
- #user service
- db_user_count="$(/usr/bin/who|wc -l)"
- db_user_info="$(/usr/bin/who)"
- #user log
- user_worklog='/usr/local/monitor/logs/user_work_log'
- user_downlog='/usr/local/monitor/logs/user_down_log'
- #disk service
- alert_disk='80'
- db_disk="$(/bin/df -H|sort -k5nr|grep -vE 'Filesystem|tmpfs|cdrom'|awk '{print $5 " " $1}')"
- db_disk_use=$(echo $db_disk|awk '{print $1}'|cut -d '%' -f1)
- db_disk_partition=$(echo $db_disk|awk '{print $2}')
- #disk log
- disk_worklog='/usr/local/monitor/logs/disk_work_log'
- disk_downlog='/usr/local/monitor/logs/disk_down_log'
- #cpu log
- cpu_worklog='/usr/local/monitor/logs/cpu_work_log'
- cpu_downlog='/usr/local/monitor/logs/cpu_down_log'
- #notification mail
- email='[email protected]'
- #monitor
- if [ ! -d "$loghere" ];then
- mkdir $loghere
- fi
- #check cpu_idle
- #check cpu_idle
- if [ $db_cpu -ge $alert_cpu ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $db_cpu"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: cpu_use was Exceed Threshold value: 80%" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $db_cpu" >> $cpu_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$db_name','$db_ip','$monitor_name','cpu_use','$alert_cpu','$db_cpu','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: cpu_idle Monitor_Server: $monitor_name Cpu_use: $db_cpu" >> $cpu_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $cpu_table values ('','$here','$db_name','$db_ip','$monitor_name','cpu_use','$alert_cpu','$db_cpu','normal',now())";
- fi
- #check memory
- if [ $db_mem -le $alert_mem ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: memory Monitor_Server: $monitor_name Free_mem: $db_mem"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: memory was Exceed Threshold value" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: memory Monitor_Server: $monitor_name Free_mem: $db_mem" >> $memory_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$db_name','$db_ip','$monitor_name','memory','$alert_mem','$db_mem','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: memory Monitor_Server: $monitor_name Free_mem: $db_mem" >> $memory_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$db_name','$db_ip','$monitor_name','memory','$alert_mem','$db_mem','normal',now())";
- fi
- #check swap
- if [ $db_swap -gt $alert_swap ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: swap Monitor_Server: $monitor_name Swap_db: $db_swap"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: swap Ip: $db_ip was Exceed Threshold value" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: swap Monitor_Server: $monitor_name Swap_db: $db_swap" >> $memory_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$db_name','$db_ip','$monitor_name','swap','$alert_swap','$db_swap','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: swap Monitor_Server: $monitor_name Swap_db: $db_swap" >> $memory_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $memory_table values ('','$here','$db_name','$db_ip','$monitor_name','swap','$alert_swap','$db_swap','normal',now())";
- fi
- #check load_15
- if [ $db_load -ge $alert_load ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $db_load_15"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: load_15 Ip: $db_ip was Exceed Threshold value" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: load_15 Monitor_Server: $monitor_name Load_use: $db_load_15" >> $load_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$db_name','$db_ip','$monitor_name','load_15','$alert_load','$db_load_15','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $server Ip: $ip Service: load Monitor_Server: $monitor_name Load_use: $db_load_15" >> $load_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $load_table values ('','$here','$db_name','$db_ip','$monitor_name','load_15','$alert_load','$db_load_15','normal',now())";
- fi
- #check io_idle
- if [ $db_io_idle -ge $alert_io ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $db_io_idle"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: io_use was Exceed Threshold value: 80%" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $db_io_idle" >> $io_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$db_name','$db_ip','$monitor_name','io_use','$alert_io','$db_io_idle','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: io_idle Monitor_Server: $monitor_name Io_use: $db_io_idle" >> $io_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $io_table values ('','$here','$db_name','$db_ip','$monitor_name','io_use','$alert_io','$db_io_idle','normal',now())";
- fi
- #check hareware error info
- if [ $db_hardware_error -gt 0 ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: hardware_error Monitor_Server: $monitor_name Error: $db_info_error"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: hardware_error were some hardware imformation error" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: hardware_error Monitor_Server: $monitor_name Error: $db_info_error" >> $hard_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$db_name','$db_ip','$monitor_name','hardware_error','0','$db_hardware_error','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: hardware_error Monitor_Server: $monitor_name Error: Nothing" >> $hard_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $hardware_table values ('','$here','$db_name','$db_ip','$monitor_name','hardware_error','0','$db_hardware_error','normal',now())";
- fi
- #check message error
- if [ $db_message_error_count -ge 1 ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: message_error Monitor_Server: $monitor_name Message_error: $db_message_error"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: message_error were some message imformation error" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: message_error Monitor_Server: $monitor_name Message_error: $db_message_error" >> $message_downlog-$day
- /usr/bin/mysql-h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $message_table values ('','$here','$db_name','$db_ip','$monitor_name','message_error','1','$db_message_error_count','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: message_error Monitor_Server: $monitor_name Message_error: Nothing" >> $message_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $message_table values ('','$here','$db_name','$db_ip','$monitor_name','message_error','1','$db_message_error_count','normal',now())";
- fi
- #check user
- if [ $db_user_count -ge 3 ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: user Monitor_Server: $monitor_name User: $db_user_info"|/bin/mail -s "Notification ShengFen: $here Server: $db_name Service: user was Exceed Threshold value: 3" $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: user Monitor_Server: $monitor_name User: $db_user_info" >> $user_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$db_name','$db_ip','$monitor_name','user','3','$db_user_count','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: user Monitor_Server: $monitor_name User: normal" >> $user_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $user_table values ('','$here','$db_name','$db_ip','$monitor_name','user','3','$db_user_count','normal',now())";
- fi
- #check disk
- if [ $db_disk_use -ge $alert_disk ];then
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $db_disk_partition ($db_disk_use%)"|/bin/mail -s "Warning!!! $here Server: $db_name Service: disk_use was Exceed Threshold value : $alert_disk% " $email
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $db_disk_partion ($db_disk_use%)" >> $disk_downlog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $disk_table values ('','$here','$db_name','$db_ip','$monitor_name','disk_use','$alert_disk','$db_disk_partition','$db_disk_use','abnormal',now())";
- else
- echo "$now ShengFen: $here Server: $db_name Ip: $db_ip Service: disk_use Monitor_Server: $monitor_name Disk_use: $db_disk_partition ($db_disk_use%)" >> $disk_worklog-$day
- /usr/bin/mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "insert into $disk_table values ('','$here','$db_name','$db_ip','$monitor_name','disk_use','$alert_disk','$db_disk_partition','$db_disk_use','normal',now())";
- fi
- 字段 类型 解释
- id int(10) 编号,主要为监控此服务的次数
- province varchar(30) 省份名称
- server varchar(30) 监控主机名称,主要是监控主机hostname的名称
- ip varchar(20) 监控主机的ip
- monitor_server varchar(30) 监控主机的类型,我这里分别有web、交换、引擎、备用、数据库
- service varchar(20) 监控的服务名称
- alert int(10) 监控服务的阀值
- value int(10) 监控服务的当前值
- status varchar(10) 监控服务器的状态,normal与abnormal
- date datetime 监控服务的时间
- 字段 类型 解释
- id int(10) 编号,主要为监控此服务的次数
- province varchar(30) 省份名称
- server varchar(30) 监控主机名称,主要是监控主机hostname的名称
- ip varchar(20) 监控主机的ip
- monitor_server varchar(30) 监控主机的类型,我这里分别有web、交换、引擎、备用、数据库
- service varchar(20) 监控的服务名称
- alert int(10) 监控服务的阀值
- partition varchar(30) 最大硬盘的分区名称
- value int(10) 监控服务的当前值
- status varchar(10) 监控服务器的状态,normal与abnormal
- date datetime 监控服务的时间
- 字段 类型 解释
- id int(10) 编号,主要为监控此服务的次数
- province varchar(30) 省份名称
- server varchar(30) 监控主机名称,主要是监控主机hostname的名称
- ip varchar(20) 监控主机的ip
- monitor_server varchar(30) 监控主机的类型,我这里分别有web、交换、引擎、备用、数据库
- service varchar(20) 监控的服务名称
- status varchar(10) 监控服务器的状态,normal与abnormal
- date datetime 监控服务的时间
- 字段 类型 解释
- id int(10) 编号,主要为监控此服务的次数
- name varchar(255) 设备的名称
- ip varchar(30) 设备的ip
- mac varchar(30) 设备的mac
- type varchar(30) 设备的类型
- jifang_name varchar(255) 机房的名称
- raw_name varchar(255) 机架的名称
- raw_location varchar(45) 机架的位置
- assets_code varchar(30) 财产编号
- state varchar(30) 设备状态
- price int(10) 设备的架构
- #!/bin/bash
- #mysql info
- mysql_ip='1.1.1.1'
- ##数据库的ip
- mysql_username='root'
- ##数据库的登录用户
- mysql_passwd='****'
- ##数据库的登录密码
- mysql_database='monitor'
- ##需要登录的数据库
- here='gansu'
- ##创建数据库的省份名称
- memory_table=''$here'_memory'
- load_table=''$here'_load'
- io_table=''$here'_io'
- hardware_table=''$here'_hardware'
- message_table=''$here'_message'
- user_table=''$here'_user'
- disk_table=''$here'_disk'
- cpu_table=''$here'_cpu'
- service_table=''$here'_service'
- ##创建数据库的表名
- #create table memory
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $memory_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table load
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $load_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table io
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $io_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table hardware
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $hardware_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table message
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $message_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table user
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $user_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table disk
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $disk_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,partition varchar(30) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table cpu
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $cpu_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,alert int(10) not null,value int(10) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
- #create table service
- mysql -h $mysql_ip -u$mysql_username -p$mysql_passwd $mysql_database -e "create table $service_table(id int(10) not null primary key auto_increment,province varchar(30) not null,server varchar(30) not null,ip varchar(20) not null,monitor_server varchar(30) not null,service varchar(20) not null,status varchar(10) not null,date datetime)ENGINE=InnoDB DEFAULT CHARSET=utf8;"
下一篇文章地址:
运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(五)
http://dl528888.blog.51cto.com/2382721/1035252