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即可。

利用nginx concat模块合并js css

如果你网站有很多css和js文件,如果把它们分别合并到一个文件,可以大大减小http请求数,加快网站访问速度。nginx-http-concat模块是由淘宝网开发的,现在淘宝网合并js和css正是用这个模块,如链接http://a.tbcdn.cn/??s/kissy/1.2.0/kissy-min.js,p/global/1.0/global-min.js,p/fp/2012/core.js,p/fp/2012/fp/module.js,p/fp/2012/fp/util.js,p/fp/2012/fp/directpromo.js?t=2012080620120924.js就是合并了kissy-min.js、global-min.js、core.js、module.js、util.js,directpromo.js等js文件。要使用这个模块,第一步当然是安装此模块并配置nginx,第二步是修改网站源码中引用js文件的代码,如本站合并js前的代码为:

  1. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/comments-ajax.js"></script>
  2. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/js/reply.js"></script>
  3. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/js/jquery.min.js" ></script>
  4. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/js/custom.js"></script>
  5. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/js/superfish.js"></script>
  6. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/js/muscript.js"></script>

合并后js代码为:

  1. <script type="text/javascript" src="http://devops.webres.wang/wp-content/themes/HotNewspro/??comments-ajax.js,js/reply.js,js/jquery.min.js,js/custom.js,js/superfish.js,js/muscript.js"></script>

下面以本博客为例讲解如何使用此模块。

安装concat模块

下载concat模块:

  1. git clone git://github.com/taobao/nginx-http-concat.git

使用–add-module添加模块:

  1. –add-module=/path/to/nginx-http-concat

配置nginx

在nginx.conf加入:

  1. location /wp-content/themes/HotNewspro {
  2. concat on;
  3. }

这段代码表示在目录/wp-content/themes/HotNewspro启用concat模块,这样在这目录的css或js才能合并。
更详细的nginx concat模块解释请移至:https://github.com/taobao/nginx-http-concat

nginx_lua模块基于mysql数据库动态修改网页内容

最近要实现一个需求,根据查询mysql数据库返回的内容,插入到网页底部。目前前端是用varnish作的缓存,刚开始想从varnish下手,但官方varnish不支持修改网页内容,要支持的话还需要自己修改源码实现,这个有点难度放弃了,于是想在varnish的后端架设一台nginx的反向代理服务器获取网页内容,然后通过nginx_lua模块实现查询mysql数据库动态修改网页的功能。下面是使用nginx_lua模块的方法,记录一下方便下次查询。

安装luajit2.0

git的安装可以参考:http://devops.webres.wang/2012/09/centos-install-git/

  1. cd /tmp/
  2. git clone http://luajit.org/git/luajit-2.0.git
  3. cd luajit-2.0/
  4. make && make install
  5. ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
  6. ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/

下载ngx_devel_kit、lua-nginx-module和nginx

  1. cd /tmp
  2. git clone http://github.com/simpl/ngx_devel_kit.git
  3. git clone http://github.com/chaoslawful/lua-nginx-module.git
  4. wget http://nginx.org/download/nginx-1.2.3.tar.gz

安装nginx

  1. tar xzf nginx-1.2.3.tar.gz
  2. cd nginx-1.2.3
  3. export LUAJIT_LIB=/usr/local/lib
  4. export LUAJIT_INC=/usr/local/include/luajit-2.0
  5. ./configure –prefix=/usr/local/nginx –add-module=/tmp/ngx_devel_kit –add-module=/tmp/lua-nginx-module
  6. make && make install

安装lua-resty-mysql

  1. cd /tmp
  2. git clone http://github.com/agentzh/lua-resty-mysql.git
  3. cd lua-resty-mysql/
  4. make install

nginx配置文件

  1. server {
  2.         ……
  3.         ……
  4.                resolver 8.8.8.8;
  5.     location / {
  6.         default_type  text/html;
  7.         content_by_lua_file "/usr/local/nginx/conf/lua";
  8. }
  9.     location /proxy {
  10.                 internal;
  11.                 proxy_pass $scheme://$http_host$request_uri;
  12.                 proxy_set_header Accept-Encoding ”;
  13.                  proxy_redirect          off;
  14.     }
  15.        ……
  16.        ……
  17.          }

lua内容

  1. if (ngx.req.get_method()=="GET") then
  2.         data=ngx.location.capture("/proxy")
  3.         if data.status == 200 then
  4.                 if data.header["Content-Type"] and string.find(data.header["Content-Type"],"html") then
  5.  
  6.                         –mysql connect begin
  7.                         local mysql = require "resty.mysql"
  8.                         local db, err = mysql:new()
  9.                         if not db then
  10.                                 ngx.say("failed to instantiate mysql: ", err)
  11.                                 return
  12.                         end
  13.                         db:set_timeout(1000) — 1 sec
  14.                         ok, err, errno, sqlstate = db:connect{
  15.                                 host = "127.0.0.1",
  16.                                 port = 3306,
  17.                                 database = "proxy",
  18.                                 user = "root",
  19.                                 password = "537817",
  20.                                 compact_arrays=true,
  21.                                 max_packet_size = 1024 * 1024 }
  22.                         if not ok then
  23.                                 ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
  24.                                 return
  25.                         end
  26.                         host=ngx.req.get_headers()["Host"]
  27.                         sql="select content from cache_modify where domain=’"..host.."’"
  28.                         res, err, errno, sqlstate = db:query(sql)
  29.                         if not res then
  30.                                 ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
  31.                                 return
  32.                         end
  33.                         –mysql connect end
  34.  
  35.                         if table.getn(res)==0 then
  36.                                 for k,v in pairs(data.header) do
  37.                                 ngx.header[k]=v
  38.                                 end
  39.                                 ngx.say(data.body)
  40.                         else
  41.                                 for k,v in pairs(data.header) do
  42.                                         if (k=="Content-Length") then
  43.                                                 ngx.header["Content-Length"]=""
  44.                                         else
  45.                                                 ngx.header[k]=v
  46.                                         end
  47.                                 end
  48.                                 body,_=string.gsub(data.body,"</body>",res[1][1].."</body>")
  49.                                 ngx.say(body)
  50.                         end
  51.                 else
  52.                         for k,v in pairs(data.header) do
  53.                                 ngx.header[k]=v
  54.                         end
  55.                         ngx.say(data.body)
  56.                 end;
  57.         else
  58.                 ngx.exec("/proxy")
  59.         end;
  60. else
  61.                 ngx.exec("/proxy");
  62. end;

模块地址:http://wiki.nginx.org/HttpLuaModule

centos安装git

1、安装依赖

  1. yum -y install curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-devel autoconf

2、开始安装

  1. wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
  2. tar xzvf git-latest.tar.gz
  3. cd git-`date +%F`
  4. autoconf
  5. ./configure
  6. make && make install

3、查看版本

  1. git –version

git version 1.7.12.GIT

NTP时间服务器工作原理

NTP简介

NTP(Network Time Protocol, 网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步,NTP基于UDP保温进行传输,使用UDP端口号为123。
使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。
对于运行NTP的本地系统,既可以接受来自其他时钟源的同步,又可以作为时钟源同步其他的时钟,并且可以喝其他设备互相同步。

NTP工作原理

NTP的基本工作原理如图1-1所示。Device A和Device B通过网络相连,他们都有自己独立的系统时钟,需要通过NTP实现各自系统时钟的自动同步。为便于理解,作如下假设:

  • 在Device A和Device B的系统时钟同步之前,DeviceA的时钟设定为10:00:00am,DeviceB的时钟设定为11:00:00am。
  • 设备B作为NTP时间服务器,即设备A将使自己的时钟与设备B的时钟同步。
  • NTP报文在设备A和设备B之间单向传输所需要的时间是1秒
  • FAQ

    图1-1 NTP原理图
    系统时钟同步过程如下:

  • 设备A发送一个NTP报文给设备B,该报文带有它离开设备A时的时间戳,该时间戳为10:00:00am(T1)。
  • 当此NTP报文到达设备B时,设备B加上自己的时间戳,该时间戳为11:00:01am(T2)。
  • 当此NTP报文离开设备B时,设备B再加上自己的时间戳,改时间戳为11:00:02(T3)。
  • 当设备A接收到该响应报文时,设备A的本地时间为10:00:03am(T4)。
  • 至此,设备A已经拥有足够的信息来计算两个重要的参数:
  • NTP报文的往返时延Delay=(T4-T1)-(T3-T2)=2 秒。
  • 设备A相对设备B的时间差offset=((T2-T1)+(T3-T4))/2=1小时。
  • 这样,设备A就能够根据这些信息来设定自己的时钟,使之与设备B的时钟同步。
    以上内容只是对NTP工作原理的一个粗略描述,更详细的资料可以参阅RFC 1305。

    NTP工作模式

    设备可以采用多种NTP工作模式进行时间同步:

  • 客户端/服务端模式
  • 对等体模式
  • 广播模式
  • 组播模式
  • 用户可以根据需要选择合适的工作模式。在不能确定服务器或对等体IP地址、网络中需要同步的设备很多等情况下,可以通过广播或组播模式实现时钟同步;服务器和对等体模式中,设备从指定的服务器或对等体获得时钟同步,增加了时钟的可靠性。
    1、客户端/服务器模式
    FAQ
    在该模式下,客户端能同步到服务器,而服务器无法同步到客户端。Mode3客户模式,mode4服务模式,
    适用于一台时间服务器接收上层时间服务器的时间信息,并提供时间信息给下层的用户。
    2、对等模式
    FAQ
    主动对等体和被动对等体可以互相同步。如果双方的时钟都已经同步,则以层数小的时钟为准。Mode1是主对等体模式,mode2被对等体模式,mode3客户模式,mode4是服务模式
    3、广播模式
    FAQ
    在广播模式中,服务器端周期性地向广播地址255.255.255.255发送时钟同步报文,报文中的Mode字段设置为5(广播模式)。客户端侦听来自服务器的广播报文。流程如图1-4所示。Mode3客户模式,mode4服务模式
    4、组播模式
    FAQ
    在组播模式中,服务端周期性地向组播地址发送时钟同步报文。报文中的mode5是组播模式,mode4是服务器模式,mode3是客户模式。

    python MySQLdb模块介绍

    当我们需要python操作mysql数据库时,可以安装MySQLdb模块以支持mysql的操作。
    1、安装MySQLdb模块
    因为CentOS有现成的rpm包,所以我们可以选择yum安装。

    1. yum install MySQL-python

    2、第一个示例

    1. #!/usr/bin/python
    2. import MySQLdb as mdb  #导入MySQLdb模块为mdb
    3. con=None #初始化con为None
    4. con=mdb.connect(‘localhost’,’root’,’root’,’mysql’) #连接数据库,参数依次为主机,用户,密码,数据库
    5. cur=con.cursor() #取得cursor对象,为了能遍历数据。
    6. cur.execute("select version()") #执行查询语句
    7. data=cur.fetchone() #取一条数据
    8. print "Database version: %s " % data #打印数据
    9. con.close() #关闭连接

    3、创建并填充表

    1. #!/usr/bin/python
    2. import MySQLdb as mdb
    3. con = mdb.connect(‘localhost’, ‘testuser’, ‘test623’, ‘testdb’);
    4.  
    5. with con:
    6.     
    7.     cur = con.cursor()
    8.     cur.execute("CREATE TABLE IF NOT EXISTS
    9.         Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))") #创建表格
    10.     cur.execute("INSERT INTO Writers(Name) VALUES(‘Jack London’)")  #填充数据
    11.     cur.execute("INSERT INTO Writers(Name) VALUES(‘Honore de Balzac’)")
    12.     cur.execute("INSERT INTO Writers(Name) VALUES(‘Lion Feuchtwanger’)")
    13.     cur.execute("INSERT INTO Writers(Name) VALUES(‘Emile Zola’)")
    14.     cur.execute("INSERT INTO Writers(Name) VALUES(‘Truman Capote’)")

    3、检索数据

    1. #!/usr/bin/python
    2. # -*- coding: utf-8 -*-
    3.  
    4. import MySQLdb as mdb
    5. import sys
    6.  
    7.  
    8. con = mdb.connect(‘localhost’, ‘testuser’,
    9.         ‘test623’, ‘testdb’);
    10.  
    11. with con:
    12.  
    13.     cur = con.cursor()
    14.     cur.execute("SELECT * FROM Writers")
    15.  
    16.     rows = cur.fetchall()  #把所有数据以元组类型赋值给rows
    17.  
    18.     for row in rows:  #循环打印数据
    19.         print row

    MySQLdb文档:http://mysql-python.sourceforge.net/MySQLdb-1.2.2/

    cacti监控磁盘IO

    1、检查net-snmp是否支持IO监控

    1. snmpwalk -v 3 -u username -A password -a md5 -l authNoPriv localhost .1.3.6.1.4.1.2021.13.15.1.1.1

    执行如上命令,如果返回类似如下数据,则表示支持disk io的监控,否则需要重新编译增加diskio-module模块。

    1. UCD-DISKIO-MIB::diskIOIndex.1 = INTEGER: 1
    2. UCD-DISKIO-MIB::diskIOIndex.2 = INTEGER: 2
    3. UCD-DISKIO-MIB::diskIOIndex.3 = INTEGER: 3
    4. ……

    2、下载Cacti_Net-SNMP_DevIO_v3.1.zip

    下载Cacti_Net-SNMP_DevIO_v3.1.zip,解压并上传net-snmp_devio.xml到 /resource/snmp_queries/目录。

    3、导入模板

    通过cacti后台的”Import Templates”导入所有的*_TMPL.xml文件,最后导入net-snmp_devIO-Data_query.xml文件。完成后,你就可以在“Data Queries”看到“ucd/net – Get Device I/O”。

    4、为已存在的”ucd/net SNMP Host”增加磁盘IO监控。

    切换到”devices”,点击已存在的”ucd/net snmp host”主机,如我的devops.webres.wang-ucd/net-snmp。往页尾看,在“Associated Data Queries”的”Add Data Query: “中选择”ucd/net – Get Device I/O”,”Re-Index Method: “选择”Index Count Changed”,点击”Add”增加Data Queries。

    5、创建IO图形监控

    接着点击页头的”Create Graphs for this Host”,在”
    Data Query [ucd/net – Get Device I/O]”下面选择需要监控的磁盘,点击”create”开始创建图形。
    至此cacti监控磁盘IO的设置已经完成。
    参考:http://forums.cacti.net/about8777-0-asc-0.html