Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95442
  • 博文数量: 5
  • 博客积分: 361
  • 博客等级: 入伍新兵
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-16 16:10
文章分类

全部博文(5)

文章存档

2012年(5)

我的朋友

分类: LINUX

2012-10-25 18:04:04

用python写了个nagios的脚本用来监控伺服器的TCP/IP连接数,脚本写完测试无误后,nagios管理页面就是不显示数据,只显示null,而命令行直接执行返回的数据正常。

为了便于查错,需要将python脚本的错误输出重定向到文件。
修改commands.cfg为(红色加粗内容为添加的内容):

define command {
        command_name    check_snmp_tcp_v2
#        command_line    sudo $USER1$/check_snmp_tcp.py $ARG1$ $HOSTADDRESS$ $ARG2$ $ARG3$ $ARG4$ $ARG5$
        command_line    $USER1$/check_snmp_tcp.py $ARG1$ $HOSTADDRESS$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ 2>/tmp/py.log
}

等nagios重新检测后,生成了py.log日志文件,查看日志发现以下错误:
pkg_resources.ExtractionError: Can't extract file(s) to egg cache
The following error occurred while trying to extract file(s) to the Python egg
cache:
[Errno 13] Permission denied: '/root/.python-eggs'
The Python egg cache directory is currently set to:
/root/.python-eggs
Perhaps your account does not have write access to this directory? You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.


由于nagios程序配置用nagios用户执行,而.python-eggs在root的家目录,自然没有权限。以下两个方法可以解决:
方法一:

在/etc/sudoers加入:

nagios  ALL=(ALL)       NOPASSWD: /usr/local/nagios/libexec/script.py

在commands.cfg加入sudo

define command {
        command_name    check_test
        command_line       sudo $USER1$/script.py $ARG1$ $HOSTADDRESS$
}



方法二:

#echo "export PYTHON_EGG_CACHE=/tmp/.python-eggs" >> /etc/profile
#source /etc/profile
#/etc/init.d/nagios restart  

 

阅读(4876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~