diff options
| author | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-09-29 15:21:20 +0000 |
|---|---|---|
| committer | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-09-29 15:21:20 +0000 |
| commit | aa78f2b7b5db7e99db5713031b769da2e9ef673f (patch) | |
| tree | cb8ba86811395f5c862cf78f05ff24fe769de19a /src | |
| parent | 5e63748e28881095925f8183e2baccdf788b12e6 (diff) | |
Minor changes.
git-svn-id: svn://svn.zabbix.com/trunk@2084 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/zbxsysinfo/Makefile.am | 2 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/linux/file.c | 135 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/linux/param.c | 42 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/linux/regexp.c | 82 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/sysinfo.c | 8 |
5 files changed, 265 insertions, 4 deletions
diff --git a/src/libs/zbxsysinfo/Makefile.am b/src/libs/zbxsysinfo/Makefile.am index efafe645..7b30ba79 100644 --- a/src/libs/zbxsysinfo/Makefile.am +++ b/src/libs/zbxsysinfo/Makefile.am @@ -2,7 +2,7 @@ SUBDIRS= if CONFIG_HOST_LINUX libzbxsysinfo_a_SOURCES=linux/cpu.c linux/diskio.c linux/diskspace.c linux/file.c linux/inodes.c linux/memory.c linux -/sensors.c linux/swap.c linux/uptime.c sysinfo.c linux/system.c +/sensors.c linux/swap.c linux/uptime.c sysinfo.c linux/system.c linux/regexp.c linux/param.c endif if CONFIG_HOST_SOLARIS diff --git a/src/libs/zbxsysinfo/linux/file.c b/src/libs/zbxsysinfo/linux/file.c index 00d0be55..c040c141 100644 --- a/src/libs/zbxsysinfo/linux/file.c +++ b/src/libs/zbxsysinfo/linux/file.c @@ -82,6 +82,8 @@ #include "common.h" #include "sysinfo.h" +#define MAX_FILE_LEN 1024*1024 + int VFS_FILE_SIZE(const char *cmd, const char *filename,double *value) { struct stat buf; @@ -148,3 +150,136 @@ int VFS_FILE_EXISTS(const char *cmd, const char *filename,double *value) /* File does not exist or any other error */ return SYSINFO_RET_OK; } + +int VFS_FILE_REGEXP(const char *cmd, const char *param, char **value) +{ + char filename[MAX_STRING_LEN]; + char regexp[MAX_STRING_LEN]; + FILE *f = NULL; + char *buf = NULL; + int len; + char tmp[MAX_STRING_LEN]; + char *c; + + int ret = SYSINFO_RET_OK; + + memset(tmp,0,MAX_STRING_LEN); + + if(get_param(param, 1, filename, MAX_STRING_LEN) != 0) + { + ret = SYSINFO_RET_FAIL; + } + + if(get_param(param, 2, regexp, MAX_STRING_LEN) != 0) + { + ret = SYSINFO_RET_FAIL; + } + + if(ret == SYSINFO_RET_OK) + { + f=fopen(filename,"r"); + if(f==NULL) + { + ret = SYSINFO_RET_FAIL; + } + } + + if(ret == SYSINFO_RET_OK) + { + buf=(char *)malloc((size_t)MAX_FILE_LEN); + if(buf == NULL) + { + ret = SYSINFO_RET_FAIL; + } + else + { + memset(buf,0,100); + } + } + + + if(ret == SYSINFO_RET_OK) + { + if(0 == fread(buf, 1, MAX_FILE_LEN-1, f)) + { + ret = SYSINFO_RET_FAIL; + } + } + + if(buf != NULL) + { + free(buf); + } + + if(f != NULL) + { + close(f); + } + + c=zbx_regexp_match(buf, regexp, &len); + + if(c == NULL) + { + tmp[0]=0; + } + else + { + strncpy(tmp,c,len); + } + + *value = strdup(tmp); + + return ret; +} + +char *zbx_regexp_match(const char *string, char *pattern, int *len) +{ + int status; + char *c; + + regex_t re; + regmatch_t match; + + char c[1024]; + + *len=0; + + if (regcomp(&re, pattern, REG_EXTENDED | REG_ICASE | REG_NEWLINE) != 0) + { + return(NULL); + } + + status = regexec(&re, string, (size_t) 1, &match, 0); + + /* Not matched */ + if (status != 0) + { + return(NULL); + } + + c=string+match.rm_so; + *len=match.rm_eo - match.rm_so; + + regfree(&re); + + return c; +} + + + + + + + + + + + + + + return ret; +} + +int VFS_FILE_REGMATCH(const char *cmd, const char *filename,double *value) +{ +} diff --git a/src/libs/zbxsysinfo/linux/param.c b/src/libs/zbxsysinfo/linux/param.c new file mode 100644 index 00000000..cb2803d5 --- /dev/null +++ b/src/libs/zbxsysinfo/linux/param.c @@ -0,0 +1,42 @@ +/* +** ZABBIX +** Copyright (C) 2000-2005 SIA Zabbix +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +**/ + +#include "config.h" + +int get_param(char *param, int num, char *buf, int maxlen) +{ + char tmp[MAX_STRING_LEN]; + char *s; + int ret = 1; + int i; + + strscpy(tmp,param); + s=(char *)strtok(tmp,","); + while(s!=NULL) + { + i++; + if(i == num) + { + strncpy(buf,s,maxlen); + } + s=(char *)strtok(NULL,";"); + } + + return ret; +} diff --git a/src/libs/zbxsysinfo/linux/regexp.c b/src/libs/zbxsysinfo/linux/regexp.c new file mode 100644 index 00000000..1f8661bf --- /dev/null +++ b/src/libs/zbxsysinfo/linux/regexp.c @@ -0,0 +1,82 @@ +#include "config.h" + +#include <stdio.h> +#include <regex.h> +#include <stdio.h> +#include <errno.h> +#include <stdlib.h> + + +#define MAX_FILE_LEN 1024*1024 + + +int main (int argc, char *argv[]){ + + int x; + FILE *f; + char *buf; + + f=fopen(argv[1],"r"); + if(f==NULL) + { + printf("Error fopen(%s)\n", strerror(errno)); + return 0; + } + + buf=(char *)malloc((size_t)100); + + memset(buf,0,100); + + x=fread(buf, 1, 100, f); + + if(x==0) + { + printf("Error fread(%s)\n", strerror(errno)); + } + + printf("Read [%d] bytes\n", x); + + + + x = match(buf, argv[2]); + + if ( x == 1 ){ + + printf("\n\n Match \n\n"); + } + + return(0); +} + +char *zbx_regexp_match(const char *string, char *pattern, int *len) +{ + int status; + char *c; + + regex_t re; + regmatch_t match; + + char c[1024]; + + *len=0; + + if (regcomp(&re, pattern, REG_EXTENDED | REG_ICASE | REG_NEWLINE) != 0) + { + return(NULL); + } + + status = regexec(&re, string, (size_t) 1, &match, 0); + + /* Not matched */ + if (status != 0) + { + return(NULL); + } + + c=string+match.rm_so; + *len=match.rm_eo - match.rm_so; + + regfree(&re); + + return c; +} diff --git a/src/libs/zbxsysinfo/sysinfo.c b/src/libs/zbxsysinfo/sysinfo.c index 07e61010..042f1330 100644 --- a/src/libs/zbxsysinfo/sysinfo.c +++ b/src/libs/zbxsysinfo/sysinfo.c @@ -234,12 +234,14 @@ COMMAND agent_commands[]= {"vfs.fs.inode.total[*]",VFS_FS_INODE_TOTAL, 0, "/"}, {"vfs.fs.inode.pfree[*]",VFS_FS_INODE_PFREE, 0, "/"}, - {"vfs.file.atime[*]" ,VFS_FILE_ATIME, 0, "/etc/passwd"}, - {"vfs.file.cksum[*]" ,VFS_FILE_CKSUM, 0, "/etc/services"}, - {"vfs.file.ctime[*]" ,VFS_FILE_CTIME, 0, "/etc/passwd"}, + {"vfs.file.atime[*]" ,VFS_FILE_ATIME, 0, "/etc/passwd"}, + {"vfs.file.cksum[*]" ,VFS_FILE_CKSUM, 0, "/etc/services"}, + {"vfs.file.ctime[*]" ,VFS_FILE_CTIME, 0, "/etc/passwd"}, {"vfs.file.exists[*]" ,VFS_FILE_EXISTS, 0, "/etc/passwd"}, {"vfs.file.md5sum[*]" ,0, VFS_FILE_MD5SUM, "/etc/services"}, {"vfs.file.mtime[*]" ,VFS_FILE_MTIME, 0, "/etc/passwd"}, + {"vfs.file.regexp[*]" ,0, VFS_FILE_REGEXP, "/etc/passwd,root"}, + {"vfs.file.regmatch[*]" ,VFS_FILE_REGMATCH, 0, "/etc/passwd,root"}, {"vfs.file.size[*]" ,VFS_FILE_SIZE, 0, "/etc/passwd"}, {"system.cpu.idle1" ,SYSTEM_CPU_IDLE1, 0, 0}, |
