`
zhengdl126
  • 浏览: 2509506 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

python脚本监控Linux磁盘空间使用率

 
阅读更多
【转载】http://blog.chinaunix.net/uid-26978448-id-3408364.html


#cat check_snmp_storage.py

 

 

  1. #! / usr/ bin/ python
  2. #_* _ coding: utf- 8 _* _
  3. #_* _ coding: cp950 _* _

  4. '' '
  5. Create date: 2012-10-30
  6. Last update: 2012-10-30
  7. Version: 1.0
  8. Description: Monitor Disk usage
  9. Author: Victor
  10. QQ:1409175531
  11. '''

  12. import sys
  13. import netsnmp
  14. from decimal import *


  15. def help( ) :
  16.   print '' 'Usage:
  17. sys.argv[0] <Community> <Host> <Device> <Warning_threshold> <Critical_threshold>'''


  18. if len( sys. argv) < 6:
  19.   help( )
  20.   sys. exit( 3)
  21. elif sys. argv[ 4] > sys. argv[ 5] :
  22.   print 'Critical_threshold must be more than Warning_threshold'
  23.   sys. exit( 3)


  24. try :
  25.   session = netsnmp. Session( Version= 2, Community= sys. argv[ 1] , DestHost= sys. argv[ 2] )
  26. except IndexError :
  27.   sys. exit( 3)

  28. oid01 = netsnmp. Varbind( 'hrStorageDescr' )
  29. oid02 = netsnmp. Varbind( 'hrStorageSize' )
  30. oid03 = netsnmp. Varbind( 'hrStorageUsed' )
  31. oid04 = netsnmp. Varbind( 'hrStorageAllocationUnits' )

  32. oidlist01 = netsnmp. VarList( oid01)
  33. oidlist02 = netsnmp. VarList( oid02)
  34. oidlist03 = netsnmp. VarList( oid03)
  35. oidlist04 = netsnmp. VarList( oid04)

  36. rl01 = session. walk( oidlist01)
  37. rl02 = session. walk( oidlist02)
  38. rl03 = session. walk( oidlist03)
  39. rl04 = session. walk( oidlist04)

  40. try :
  41.   units = dict( zip( rl01, rl04) ) [ sys. argv[ 3] ]
  42.   total = Decimal( dict( zip( rl01, rl02) ) [ sys. argv[ 3] ] ) * Decimal( units) / Decimal( 1024) / Decimal( 1024)
  43.   used_1 = dict( zip( rl01, rl03) ) [ sys. argv[ 3] ]
  44.   used_2 = Decimal( used_1) * Decimal( units) / Decimal( 1024) / Decimal( 1024)
  45. except KeyError :
  46.   print 'Timeout or please check syntax/community/Host or other params.'
  47.   sys. exit( 3)

  48. getcontext( ) . prec = 2

  49. used_percent = int( Decimal( used_2) / Decimal( total) * Decimal( 100) )

  50. warning = Decimal( total) * Decimal( sys. argv[ 4] )
  51. w_threshold = int( warning)
  52. w_percent = Decimal( sys. argv[ 4] ) * Decimal( 100)

  53. critical = Decimal( total) * Decimal( sys. argv[ 5] )
  54. c_threshold = int( critical)
  55. c_percent = Decimal( sys. argv[ 5] ) * Decimal( 100)

  56. if used_percent < w_percent:
  57.   print 'OK - "%s" 使用了:%s%% | 总容量:%s MB , 已使用:%s MB (w:%s%%, c:%s%%)' % ( sys. argv[ 3] , used_percent, int( total) , int( used_2) , w_percent, c_percent)
  58.   sys. exit( 0)
  59. elif c_percent > used_percent > = w_percent:
  60.   print 'Warning - "%s" 使用了:%s%% | 总容量:%s MB , 已使用:%s MB (w:%s%%, c:%s%%)' % ( sys. argv[ 3] , used_percent, int( total) , int( used_2) , w_percent, c_percent)
  61.   sys. exit( 1)
  62. elif used_percent > c_percent:
  63.   print 'Critical - "%s" 使用了:%s%% | 总容量:%s MB , 已使用:%s MB (w:%s%%, c:%s%%)' % ( sys. argv[ 3] , used_percent, int( total) , int( used_2) , w_percent, c_percent)
  64.   sys. exit( 2)
  65. elif not used_2:
  66.   print 'Unknown'
  67.   sys. exit( 3)
  68. else :
  69.   print 'Unknown'
  70.   sys. exit( 3)

 

 

 

 

 

将脚本check_snmp_storage.py放到/usr/local/nagios/libexec目录下,给执行权限。直接执行脚本可以看到脚本的用法:

#python check_snmp_storage.py 

Usage:
sys.argv[0] <Community> <Host> <Device> <Warning_threshold> <Critical_threshold>

    <Device>  设备名,如“Physical memory”、“Swap space”、 “/usr/local”等,如果不确定设备名,可先使用snmpdf查看,如:
<Device>  需要与上图Description列的名字吻合,大小写必须一致,否则脚本通过snmp去取数据时会找不到设备。

    <Warning_threshold> 和<Critical_threshold>是报警阀值,用小数点表示,如0.8表示80%,0.9表示90%。

举例:

commands.cfg配置如下:
define command{
    command_name check_snmp_storage
    command_line $USER1$/check_snmp_storage.py $ARG1$ $HOSTADDRESS$ $ARG2$ $ARG3$ $ARG4$ $ARG5$
}

services.cfg配置如下:
define service{
        use                             service02
        host_name                   test
        service_description       Mem
        notifications_enabled     0
        check_command          check_snmp_storage_v4!public!'Physical memory'!0.85!0.9
}

以上完成后重启nagios服务,效果图如下:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics