首页 文章

Nagios插件(用C编写)无法打开文件

提问于
浏览
0

我有一个用c编写的插件,它将解析一个.log文件并确定页面命中数:

_xLogFileName = "./loging.log";
/* File operation starts here */
 _xFile = fopen ( _xLogFileName, "r" );
if ( _xFile != NULL )
{   
    //read a line upto the end of the file
    while ( fgets ( _xFileLine , sizeof  _xFileLine,  _xFile  ) != NULL ) 
    {
    // @_xTiemInStr --> cur date in YYYY-MM-DD format to identify todays log
        if(strstr(_xFileLine, _xTiemInStr) != NULL) {
            if(strstr(_xFileLine, _xLoginHitString) != NULL) {
                _xLoginPageCounter = _xLoginPageCounter + 1;
            }
        }
    }
    printf("Usage:Total Login Page Hit :%d\n",_xLoginPageCounter );
    fclose ( _xFile );
    return 0;
}
else
{
    printf("error\n");
    perror ( _xLogFileName ); 
    return 3;
}
return 0;

现在我将a.out文件放在/ usr / lib / nagios / plugin文件夹中,并将“loging.log”文件放在同一文件夹中 - 对于两个chmod 777完成 . 我可以从命令行运行插件,但是当我将它与nagios集成时,它会提供未知状态并从其他部分打印“错误” - 任何人都可以请求帮助


第二部分

另外我添加了以下代码来确定nagios的运行位置?

char cwd[1024]; 
_xLogFileName1 = "loging.log"; 
if (getcwd(cwd, sizeof(cwd)) != NULL)
   _xLogFileName = strcat( cwd,_xLogFileName1); 
   printf("FileName : %s\n", _xLogFileName);

它是在状态信息中打印/loging.log吗?

所以,我必须实际放置文件,我的nagios从/ etc / nagios3运行,我也将loging.log文件放在那里,但它仍然无法正常工作 .

更新:现在它正在工作,因为我通过c程序打印pwd并发现它从我的根(/)目录运行,所以我将loging.log文件放在那里,现在它工作正常 .

1 回答

  • 0

    您应该将完整路径传递给文件并将其存储在'_xLogFileName1'中,而不仅仅是文件名 . 强烈建议您不要将文件复制到根目录中 .

    知道网络日志,您正在寻找的日期字符串很可能位于该行的前面 . 所以我建议使用'strnstr'而不是'strstr' . 它会大大提高您的搜索速度 .

相关问题