nagios监控mysql主从状态

使用nagios监控mysql主从可以有两种方法,一种是使用nagios的nrpe插件来执行远程的shell脚本,并把数据发回监控服务器分析,二种方法是使用snmp的extend功能来执行远程脚本。我们这里介绍后一种方法。

一、mysql从服务器设置

1、mysql从服务器用户添加
执行如下语句添加用户:

  1. mysql> GRANT REPLICATION CLIENT ON *.* TO monitor@localhost IDENTIFIED BY ‘PassWord’;

2、下载check-mysql-slave.pl脚本

  1. cd /usr/local/bin/
  2. wget http://devops.webres.wang/wp-content/uploads/2012/10/check-mysql-slave.pl
  3. chmod +x check-mysql-slave.pl

3、在mysql从服务器上配置extend mysql-slave
在/etc/snmp/snmpd.conf文件的末尾添加如下代码:

  1. extend mysql-slave /usr/local/bin/check-mysql-slave.pl –user monitor –pass PassWord –sock /var/lib/mysql/mysql.sock

注意相关参数修改为自己的。
之后重载snmp:

  1. service snmpd reload

二、监控服务器设置

1、下载check_snmp_extend.sh脚本

  1. mkdir /usr/local/nagios/libexec.local
  2. cd /usr/local/nagios/libexec.local
  3. wget http://devops.webres.wang/wp-content/uploads/2012/10/check_snmp_extend.sh
  4. chmod +x check_snmp_extend.sh

2、定义USER10变量
在文件/usr/local/nagios/etc/resource.cfg添加如下变量:

  1. $USER10$=/usr/local/nagios/libexec.local

3、定义check_snmp_extend命令
在/usr/local/nagios/etc/objects/commands.cfg添加:

  1. define command{
  2. command_name check_snmp_extend
  3. command_line $USER10$/check_snmp_extend.sh $HOSTADDRESS$ $ARG1$
  4. }

4、定义监控mysql主从服务
在主机配置文件,如/usr/local/nagios/etc/objects/devops.webres.wang.cfg中添加如下service(注意,此www.cnetos.bz.cfg文件已经在nagios.cfg配置文件中包含)

  1. define host{
  2.         use                     linux-server
  3.         host_name               devops.webres.wang
  4.         alias                   devops.webres.wang
  5.         address                 142.4.33.74
  6.         }
  7. ……
  8. ……
  9. define service{
  10. ## This is an example service configured as
  11. ## extend servicename /path/to/service-check.sh
  12. ## on remote.server in /etc/snmp/snmpd.conf
  13. use generic-service
  14. host_name devops.webres.wang
  15. service_description mysql slave status
  16. check_command check_snmp_extend!mysql-slave
  17. }

参考:http://www.logix.cz/michal/devel/nagios/

nagios snmp监控服务常用command定义

  1. # ‘check_system’ command definition
  2. define command{
  3. command_name check_system
  4. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o sysDescr.0
  5. }
  6.  
  7. # ‘snmp_load’ command definition
  8. define command{
  9. command_name snmp_load
  10. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.2021.10.1.5.1,.1.3.6.1.4.1.2021.10.1.5.2,.1.3.6.1.4.1.2021.10.1.5.3 -w :’$ARG2$’,:’$ARG3$’,:’$ARG4$’ -c :’$ARG5$’,:’$ARG6$’,:’$ARG7$’ -l load
  11. }
  12.  
  13.  
  14. # ‘snmp_cpustats’ command definition
  15. define command{
  16. command_name snmp_cpustats
  17. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.2021.11.9.0,.1.3.6.1.4.1.2021.11.10.0,.1.3.6.1.4.1.2021.11.11.0 -l ‘CPU usage (user system idle)’ -u ‘%’
  18. }
  19.  
  20.  
  21. # ‘snmp_procname’ command definition
  22. define command{
  23. command_name snmp_procname
  24. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.2021.2.1.5.’$ARG2$’ -w ‘$ARG3$’:’$ARG4$’ -c ‘$ARG5$’:’$ARG6$’
  25. }
  26.  
  27.  
  28. # ‘snmp_disk’ command definition
  29. define command{
  30. command_name snmp_disk
  31. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.2021.9.1.7.’$ARG2$’,.1.3.6.1.4.1.2021.9.1.9.’$ARG2$’ -w ‘$ARG3$’:,:’$ARG4$’ -c ‘$ARG5$’:,:’$ARG6$’ -u ‘kB free (‘,’% used)’ -l ‘disk space’
  32. }
  33.  
  34.  
  35. # ‘snmp_mem’ command definition
  36. define command{
  37. command_name snmp_mem
  38. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.2021.4.6.0,.1.3.6.1.4.1.2021.4.5.0 -w ‘$ARG2$’: -c ‘$ARG3$’:
  39. }
  40.  
  41. # ‘snmp_uptime’ command definition
  42. define command{
  43. command_name snmp_uptime
  44. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o sysUpTime.0
  45.         }
  46.  
  47. # ‘snmp_swap’ command definition
  48. define command{
  49. command_name snmp_swap
  50. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.2021.4.4.0,.1.3.6.1.4.1.2021.4.3.0 -w ‘$ARG2$’: -c ‘$ARG3$’:
  51. }
  52.  
  53.  
  54. # ‘snmp_procs’ command definition
  55. define command{
  56. command_name snmp_procs
  57. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrSystem.hrSystemProcesses -w :’$ARG2$’ -c :’$ARG3$’ -l processes
  58. }
  59.  
  60.  
  61. # ‘snmp_users’ command definition
  62. define command{
  63. command_name snmp_users
  64. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrSystem.hrSystemNumUsers -w :’$ARG2$’ -c :’$ARG3$’ -l users
  65. }
  66.  
  67.  
  68. # ‘snmp_mem2’ command definition
  69. define command{
  70. command_name snmp_mem2
  71. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageUsed.’$ARG2$’,host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageSize.’$ARG2$’ -w ‘$ARG3$’ -c ‘$ARG4$’
  72. }
  73.  
  74.  
  75. # ‘snmp_swap2’ command definition
  76. define command{
  77. command_name snmp_swap2
  78. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageUsed.’$ARG2$’,host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageSize.’$ARG2$’ -w ‘$ARG3$’ -c ‘$ARG4$’
  79. }
  80.  
  81.  
  82. # ‘snmp_mem3’ command definition
  83. define command{
  84. command_name snmp_mem3
  85. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageUsed.’$ARG2$’,host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageSize.’$ARG2$’ -w ‘$ARG3$’ -c ‘$ARG4$’
  86. }
  87.  
  88.  
  89. # ‘snmp_swap3’ command definition
  90. define command{
  91. command_name snmp_swap3
  92. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageUsed.’$ARG2$’,host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageSize.’$ARG2$’ -w ‘$ARG3$’ -c ‘$ARG4$’
  93. }
  94.  
  95.  
  96. # ‘snmp_disk2’ command definition
  97. define command{
  98. command_name snmp_disk2
  99. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o host.hrStorage.hrStorageTable.hrStorageEntry.hrStorageUsed.’$ARG2$’ -w ‘$ARG3$’ -c ‘$ARG4$’
  100. }
  101.  
  102.  
  103. # ‘snmp_tcpopen’ command definition
  104. define command{
  105. command_name snmp_tcpopen
  106. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o tcp.tcpCurrEstab.0 -w ‘$ARG2$’ -c ‘$ARG3$’
  107. }
  108.  
  109.  
  110. # ‘snmp_tcpstats’ command definition
  111. define command{
  112. command_name snmp_tcpstats
  113. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o tcp.tcpActiveOpens.0,tcp.tcpPassiveOpens.0,tcp.tcpInSegs.0,tcp.tcpOutSegs.0,tcp.tcpRetransSegs.0 -l ‘TCP stats’
  114. }
  115.  
  116.  
  117. # ‘check_snmp_bgpstate’ command definition
  118. define command{
  119. command_name check_snmp_bgpstate
  120. command_line /usr/lib/nagios/plugins/check_bgpstate ‘$HOSTADDRESS$’ -c ‘$ARG1$’
  121. }
  122.  
  123.  
  124. # ‘check_netapp_uptime’ command definition
  125. define command{
  126. command_name check_netapp_uptime
  127. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.2.1.1.3.0 –delimiter=’)’ -l "Uptime is"
  128. }
  129.  
  130.  
  131. # ‘check_netapp_cpuload’ command definition
  132. define command{
  133. command_name check_netapp_cpuload
  134. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.789.1.2.1.3.0 -w 90 -c 95 -u ‘%’ -l "CPU LOAD "
  135. }
  136.  
  137.  
  138. # ‘check_netapp_numdisks’ command definition
  139. define command{
  140. command_name check_netapp_numdisks
  141. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.789.1.6.4.1.0,.1.3.6.1.4.1.789.1.6.4.2.0,.1.3.6.1.4.1.789.1.6.4.8.0,.1.3.6.1.4.1.789.1.6.4.7.0 -u ‘Total Disks’,’Active’,’Spare’,’Failed’ -l ""
  142. }
  143.  
  144.  
  145. # ‘check_compaq_thermalCondition’ command definition
  146. define command{
  147. command_name check_compaq_thermalCondition
  148. command_line /usr/lib/nagios/plugins/check_snmp -H ‘$HOSTADDRESS$’ -C ‘$ARG1$’ -o .1.3.6.1.4.1.232.6.2.1.0,.1.3.6.1.4.1.232.6.2.2.0,.1.3.6.1.4.1.232.6.2.3.0,.1.3.6.1.4.1.232.6.2.4.0 -u ‘ThermalCondition’,’ThermalTemp’,’ThermalSystem’,’ThermalCPUFan’ -w 2:2,2:2,2:2,2:2 -c 1:2,1:2,1:2,1:2 -l "Thermal status "
  149. }

转载自:https://nsrc.org/workshops/configs/2010/apricot/etc/nagios-plugins/config/snmp.cfg

Linux下python升级

安装python2.7

  1. cd /tmp
  2. wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
  3. tar xzf Python-2.7.3.tgz
  4. cd Python-2.7.3
  5. ./configure && make && make install
  6. ln -sf /usr/local/bin/python /usr/bin/python
  1. [root@localhost ~]# python -V
  2. Python 2.7.3

修复yum
编辑/usr/bin/yum,把/usr/bin/python 改为/usr/bin/python2.4

使用UglifyJS压缩javascript代码

之前是想通过nginx_lua模块简单地对js进行压缩,但是在第一步删除js的单行注释就难倒了我,通过正则我想是处理不了的,只好去搜索有哪些好用的js压缩软件,找到了UglifyJS这个基于node.js的js压缩引擎。这个工具可以在linux命令行使用,也提供了NodeJS的API。我们这里是通过lua的os.popen直接调用uglifyjs命令来压缩js。

一、Node.js安装

  1. cd /tmp
  2. wget http://nodejs.org/dist/v0.8.12/node-v0.8.12.tar.gz
  3. tar xzf node-v0.8.12.tar.gz
  4. cd node-v0.8.12
  5. ./configure && make && make install

注意:需要python的版本为2.6以上,否则会出现错误:

  1. File "./configure", line 330
  2.     o[‘default_configuration’] = ‘Debug’ if options.debug else ‘Release’
  3.                                           ^
  4. SyntaxError: invalid syntax

升级python可以参考:http://devops.webres.wang/2012/10/linux-python-upgrade/

二、安装UglifyJS

  1. npm install uglify-js

有可能出现的问题:
1、ImportError: No module named bz2
解决方法:

  1. yum -y install bzip2 bzip2-devel
  2. cd /tmp/Python-2.7.3/Modules/zlib
  3. ./configure && make && make install
  4. cd ../../
  5. python setup.py install

三、UglifyJS使用

1、使用命令行
语法:uglifyjs [ options… ] [ filename ]
详细的用法:https://github.com/mishoo/UglifyJS
2、使用node.js http
server.js脚本内容:

  1. var http = require("http");
  2. var jsp = require("uglify-js").parser;
  3. var pro = require("uglify-js").uglify;
  4. function onRequest(request, response) {
  5.   var postData = "";
  6.   request.setEncoding("utf8");
  7.   request.addListener("data", function(postDataChunk) {
  8.       postData += postDataChunk;
  9.   });
  10.  
  11.   request.addListener("end", function() {
  12.       var orig_code = postData;
  13.       var ast = jsp.parse(orig_code);
  14.       ast = pro.ast_mangle(ast);
  15.       ast = pro.ast_squeeze(ast);
  16.       var final_code = pro.gen_code(ast);
  17.   response.writeHead(200, {"Content-Type": "text/plain"});
  18.   response.write(final_code);
  19.   response.end();
  20.   });
  21. }
  22.  
  23. http.createServer(onRequest).listen(8888,"127.0.0.1");
  24.  
  25. console.log("Server has started.");

执行node server.js即可。