更新至1.8版本。
重新组织目录结构。更新至1.8版本。 git-svn-id: http://172.17.0.253/svn/soft_pkgs/sys_nicmonitor@1398 09c3743a-b0dd-45d3-b506-aa74c7a2a6ef
This commit is contained in:
18
code/tags/sys_nicmonitor-1.8/bin/README
Normal file
18
code/tags/sys_nicmonitor-1.8/bin/README
Normal file
@@ -0,0 +1,18 @@
|
||||
========
|
||||
安装说明:
|
||||
直接运行命令:
|
||||
sh ./install.sh
|
||||
|
||||
或者采用如下步骤:
|
||||
|
||||
1)安装,以root的用户执行安装命令:
|
||||
pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装
|
||||
卸载命令: pkgrm sys_nicmonitor
|
||||
更新方式安装命令:
|
||||
pkgadd -u sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
|
||||
2)查看安装是否成功
|
||||
pkginfo -i|grep sys_nicmonitor
|
||||
或者
|
||||
查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否
|
||||
4
code/tags/sys_nicmonitor-1.8/bin/install.sh
Executable file
4
code/tags/sys_nicmonitor-1.8/bin/install.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
pkgrm sys_nicmonitor
|
||||
pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
|
||||
Binary file not shown.
BIN
code/tags/sys_nicmonitor-1.8/doc/网卡监视程序动态库使用手册【新】.doc
Normal file
BIN
code/tags/sys_nicmonitor-1.8/doc/网卡监视程序动态库使用手册【新】.doc
Normal file
Binary file not shown.
BIN
code/tags/sys_nicmonitor-1.8/doc/网卡监视程序安装说明以及使用手册【新】.doc
Normal file
BIN
code/tags/sys_nicmonitor-1.8/doc/网卡监视程序安装说明以及使用手册【新】.doc
Normal file
Binary file not shown.
@@ -5,17 +5,16 @@
|
||||
# Depends on: libart_lgpl libcgi libgd pango python tcl intltool
|
||||
|
||||
name=sys_nicmonitor
|
||||
version=1.0
|
||||
release=x86_64-linx-Rocky4.2
|
||||
source=(http://172.16.0.73/mnic/$name-$version.tar.gz)
|
||||
version=1.8
|
||||
release=x86_64-Linx-Rocky4.2
|
||||
source=/home/d5000/d5000/linx/1.8/$name-$version.tar.gz
|
||||
|
||||
build() {
|
||||
cd $name-$version
|
||||
|
||||
make
|
||||
make DESTDIR=$PKG install
|
||||
#log-dir has been created in program, so comment next lines
|
||||
#mkdir -p $PKG/var/log/netcard
|
||||
#chmod 777 $PKG/var/log/netcard
|
||||
# mkdir -p $PKG/var/log/netcard
|
||||
# chmod 777 $PKG/var/log/netcard
|
||||
}
|
||||
|
||||
65
code/tags/sys_nicmonitor-1.8/src/README1.8
Normal file
65
code/tags/sys_nicmonitor-1.8/src/README1.8
Normal file
@@ -0,0 +1,65 @@
|
||||
=========
|
||||
编译说明:
|
||||
在Rocky 4.2上编译
|
||||
1)修改Pkgfile中的source指向的源文件位置
|
||||
2)然后执行: sh build.sh
|
||||
将在当前的目录下生成安装包:
|
||||
sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
|
||||
========
|
||||
安装说明:
|
||||
1)安装,以root的用户执行安装命令:
|
||||
pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装
|
||||
卸载命令: pkgrm sys_nicmonitor
|
||||
更新方式安装命令:
|
||||
pkgadd -u sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
|
||||
2)查看安装是否成功
|
||||
pkginfo -i|grep sys_nicmonitor
|
||||
或者
|
||||
查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否
|
||||
|
||||
========
|
||||
版本说明:
|
||||
--V1.8 --
|
||||
2013-05-27
|
||||
对系统高磁盘IO情况下,应用如果进行IO将可能会被阻塞。本版本为此作出调整。
|
||||
cs-fes1上编译测试OK。
|
||||
|
||||
1) 去掉shm和sem的实体文件
|
||||
#define SHM_PATH "$D5000_HOME/share/sys_netcard_shm_path"
|
||||
#define SEM_PATH "$D5000_HOME/share/sys_netcard_sem_path"
|
||||
|
||||
用key=0x1d5200 代替 sem
|
||||
用key=0x1d5010 代替 shm
|
||||
|
||||
|
||||
2)去掉动态库中get_nic_info接口中,判断路径的opendir调用,采用实际路径
|
||||
上层应用每秒可能调用3次这个接口,每判断一个网卡都调用一次,判断都个网卡的状态时就调用多次。
|
||||
|
||||
可改进:一次调用get_nic_info接口,可输入多个网卡,一次返回所有网卡状态信息。
|
||||
|
||||
3) record_log函数为日志函数,每次写一次,都重新打开一次文件。
|
||||
修改为线程模式,当记录日志时,不会被磁盘IO阻塞。
|
||||
|
||||
4) 安装目录为d5000用户的HOME目录下的lib和bin目录
|
||||
|
||||
----
|
||||
|
||||
|
||||
V1.6与V1.7差异
|
||||
共享内存路径修改:
|
||||
由/home/d5000/someone/tmp/sys_netcard_shm_path 改为 /home/d5000/someone/share/sys_netcard_shm_path
|
||||
信号量路径修改:
|
||||
由/home/d5000/someone/tmp/sys_netcard_sem_path 改为 /home/d5000/someone/share/sys_netcard_sem_path
|
||||
|
||||
注意:在编译过程中,需要动态看libman.so,它存放在目录 tags/下面 ,在进行编译的时候需要将其拷贝到
|
||||
/lib64/目录下,编译的基础环境是 Rocky 4.2 系统。
|
||||
|
||||
pkgmk命令的使用:
|
||||
将源代码打包成name-version.tar.gz样式,编写Pkgfile文件。
|
||||
使用命令:
|
||||
pkgmk -um #更新软件包的md5值
|
||||
pkgmk -kw #制作pkg安装包
|
||||
|
||||
0
code/tags/sys_nicmonitor-1.8/src/V1.8
Normal file
0
code/tags/sys_nicmonitor-1.8/src/V1.8
Normal file
7
code/tags/sys_nicmonitor-1.8/src/build.sh
Normal file
7
code/tags/sys_nicmonitor-1.8/src/build.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
pkgmk -um
|
||||
|
||||
#For debug
|
||||
#pkgmk -kw -pa post_add.sh
|
||||
|
||||
#For release
|
||||
pkgmk -pa post_add.sh
|
||||
16
code/tags/sys_nicmonitor-1.8/src/post_add.sh
Normal file
16
code/tags/sys_nicmonitor-1.8/src/post_add.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
#echo $HOME
|
||||
DFDIR=`cat /etc/passwd | grep d5000 | cut -d ":" -f 6 `
|
||||
#LOGDIR=$DFDIR/var/log/netcard
|
||||
#mkdir -p $LOGDIR
|
||||
echo $DFDIR
|
||||
#echo $LOGDIR
|
||||
|
||||
echo "$DFDIR/lib/libnic_shm.so"
|
||||
echo "$DFDIR/bin/sys_nicmonitor"
|
||||
|
||||
mv /usr/lib64/libnic_shm.so $DFDIR/lib
|
||||
mv /usr/bin/sys_nicmonitor $DFDIR/bin
|
||||
|
||||
chown d5000.d5000 $DFDIR/lib/libnic_shm.so
|
||||
chown d5000.d5000 $DFDIR/bin/sys_nicmonitor
|
||||
BIN
code/tags/sys_nicmonitor-1.8/src/sys_nicmonitor-1.8.tar.gz
Normal file
BIN
code/tags/sys_nicmonitor-1.8/src/sys_nicmonitor-1.8.tar.gz
Normal file
Binary file not shown.
8
code/tags/sys_nicmonitor-1.8/test/Makefile
Normal file
8
code/tags/sys_nicmonitor-1.8/test/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
all:read_shm recv_msg getmem
|
||||
|
||||
getmem:getmem.c
|
||||
# g++ -g -L ../dynamic_lib -lnic_shm -o $@ $^
|
||||
g++ -g -L ${HOME}/lib -lpthread -lnic_shm -o $@ $^
|
||||
clean:
|
||||
rm getmem read_shm recv_msg
|
||||
264
code/tags/sys_nicmonitor-1.8/test/dotconf.h
Normal file
264
code/tags/sys_nicmonitor-1.8/test/dotconf.h
Normal file
@@ -0,0 +1,264 @@
|
||||
#ifndef DOTCONF_H
|
||||
#define DOTCONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* stdio.h should be included by the application - as the manual page says */
|
||||
#ifndef _STDIO_H
|
||||
#include <stdio.h> /* needed for FILE* */
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
# ifndef R_OK
|
||||
#define R_OK 0
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* some buffersize definitions */
|
||||
#define CFG_BUFSIZE 4096 /* max length of one line */
|
||||
#define CFG_MAX_OPTION 32 /* max length of any option name */
|
||||
#define CFG_MAX_VALUE 4064 /* max length of any options value */
|
||||
#define CFG_MAX_FILENAME 256 /* max length of a filename */
|
||||
#define CFG_VALUES 16 /* max # of arguments an option takes */
|
||||
|
||||
#define CFG_INCLUDEPATH_ENV "DC_INCLUDEPATH"
|
||||
#define WILDCARDS "*?" /* list of supported wild-card characters */
|
||||
|
||||
/* constants for type of option */
|
||||
#define ARG_TOGGLE 0 /* TOGGLE on,off; yes,no; 1, 0; */
|
||||
#define ARG_INT 1 /* callback wants an integer */
|
||||
#define ARG_STR 2 /* callback expects a \0 terminated str */
|
||||
#define ARG_LIST 3 /* wants list of strings */
|
||||
#define ARG_NAME 4 /* wants option name plus ARG_LIST stuff */
|
||||
#define ARG_RAW 5 /* wants raw argument data */
|
||||
#define ARG_NONE 6 /* does not expect ANY args */
|
||||
|
||||
#define CTX_ALL 0 /* context: option can be used anywhere */
|
||||
|
||||
/* for convenience of terminating the dotconf_options list */
|
||||
#define LAST_OPTION { "", 0, NULL, NULL }
|
||||
#define LAST_CONTEXT_OPTION { "", 0, NULL, NULL, 0 }
|
||||
|
||||
#define DOTCONF_CB(__name) const char *__name(command_t *cmd, \
|
||||
context_t *ctx)
|
||||
#define FUNC_ERRORHANDLER(_name) int _name(configfile_t * configfile, \
|
||||
int type, long dc_errno, const char *msg)
|
||||
|
||||
|
||||
/* some flags that change the runtime behaviour of dotconf */
|
||||
#define NONE 0
|
||||
#define CASE_INSENSITIVE 1<<0 /* match option names case insensitive */
|
||||
#define DONT_SUBSTITUTE 1<<1 /* do not call substitute_env after read_arg */
|
||||
#define NO_INLINE_COMMENTS 1<<2 /* do not allow inline comments */
|
||||
#define DUPLICATE_OPTION_NAMES 1<<3 /* allow for duplicate option names */
|
||||
|
||||
/* syslog style errors as suggested by Sander Steffann <sander@steffann.nl> */
|
||||
#ifdef HAVE_SYSLOG
|
||||
#include <syslog.h>
|
||||
|
||||
#define DCLOG_EMERG LOG_EMERG /* system is unusable */
|
||||
#define DCLOG_ALERT LOG_ALERT /* action must be taken immediately */
|
||||
#define DCLOG_CRIT LOG_CRIT /* critical conditions */
|
||||
#define DCLOG_ERR LOG_ERR /* error conditions */
|
||||
#define DCLOG_WARNING LOG_WARNING /* warning conditions */
|
||||
#define DCLOG_NOTICE LOG_NOTICE /* normal but significant condition */
|
||||
#define DCLOG_INFO LOG_INFO /* informational */
|
||||
#define DCLOG_DEBUG LOG_DEBUG /* debug-level messages */
|
||||
|
||||
#define DCLOG_LEVELMASK LOG_PRIMASK /* mask off the level value */
|
||||
|
||||
#else /* HAVE_SYSLOG */
|
||||
|
||||
#define DCLOG_EMERG 0 /* system is unusable */
|
||||
#define DCLOG_ALERT 1 /* action must be taken immediately */
|
||||
#define DCLOG_CRIT 2 /* critical conditions */
|
||||
#define DCLOG_ERR 3 /* error conditions */
|
||||
#define DCLOG_WARNING 4 /* warning conditions */
|
||||
#define DCLOG_NOTICE 5 /* normal but significant condition */
|
||||
#define DCLOG_INFO 6 /* informational */
|
||||
#define DCLOG_DEBUG 7 /* debug-level messages */
|
||||
|
||||
#define DCLOG_LEVELMASK 7 /* mask off the level value */
|
||||
|
||||
#endif /* HAVE_SYSLOG */
|
||||
|
||||
/* callback types for dotconf_callback */
|
||||
|
||||
/* error constants */
|
||||
#define ERR_NOERROR 0x0000
|
||||
#define ERR_PARSE_ERROR 0x0001
|
||||
#define ERR_UNKNOWN_OPTION 0x0002
|
||||
#define ERR_WRONG_ARG_COUNT 0x0003
|
||||
#define ERR_INCLUDE_ERROR 0x0004
|
||||
#define ERR_NOACCESS 0x0005
|
||||
#define ERR_USER 0x1000 /* base for userdefined errno's */
|
||||
|
||||
/* i needed this to check an ARG_LIST entry if it's toggled in one of my apps; maybe you do too */
|
||||
#define CFG_TOGGLED(_val) ( (_val[0] == 'Y' \
|
||||
|| _val[0] == 'y') \
|
||||
|| (_val[0] == '1') \
|
||||
|| ((_val[0] == 'o' \
|
||||
|| _val[0] == 'O') \
|
||||
&& (_val[1] == 'n' \
|
||||
|| _val[1] == 'N')))
|
||||
|
||||
enum callback_types
|
||||
{
|
||||
ERROR_HANDLER = 1,
|
||||
CONTEXT_CHECKER
|
||||
};
|
||||
|
||||
typedef enum callback_types callback_types;
|
||||
typedef struct configfile_t configfile_t;
|
||||
typedef struct configoption_t configoption_t;
|
||||
typedef struct configoption_t ConfigOption;
|
||||
typedef struct command_t command_t;
|
||||
typedef void context_t;
|
||||
typedef void info_t;
|
||||
|
||||
typedef const char *(*dotconf_callback_t)(command_t *, context_t *);
|
||||
typedef int (*dotconf_errorhandler_t)(configfile_t *, int, unsigned long, const char *);
|
||||
typedef const char *(*dotconf_contextchecker_t)(command_t *, unsigned long);
|
||||
|
||||
struct configfile_t
|
||||
{
|
||||
/* ------ the fields in configfile_t are provided to the app via command_t's ; READ ONLY! --- */
|
||||
|
||||
FILE *stream;
|
||||
char eof; /* end of file reached ? */
|
||||
size_t size; /* file size; cached on-demand for here-documents */
|
||||
|
||||
context_t *context;
|
||||
|
||||
configoption_t const **config_options;
|
||||
int config_option_count;
|
||||
|
||||
/* ------ misc read-only fields ------------------------------------------------------------- */
|
||||
char *filename; /* name of file this option was found in */
|
||||
unsigned long line; /* line number we're currently at */
|
||||
unsigned long flags; /* runtime flags given to dotconf_open */
|
||||
|
||||
char *includepath;
|
||||
|
||||
/* ------ some callbacks for interactivity -------------------------------------------------- */
|
||||
dotconf_errorhandler_t errorhandler;
|
||||
dotconf_contextchecker_t contextchecker;
|
||||
|
||||
int (*cmp_func)(const char *, const char *, size_t);
|
||||
};
|
||||
|
||||
struct configoption_t
|
||||
{
|
||||
const char *name; /* name of configuration option */
|
||||
int type; /* for possible values, see above */
|
||||
dotconf_callback_t callback; /* callback function */
|
||||
info_t *info; /* additional info for multi-option callbacks */
|
||||
unsigned long context; /* context sensitivity flags */
|
||||
};
|
||||
|
||||
struct command_t
|
||||
{
|
||||
const char *name; /* name of the command */
|
||||
configoption_t *option; /* the option as given in the app; READ ONLY */
|
||||
|
||||
/* ------ argument data filled in for each line / command ----------------------------------- */
|
||||
struct {
|
||||
long value; /* ARG_INT, ARG_TOGGLE */
|
||||
char *str; /* ARG_STR */
|
||||
char **list; /* ARG_LIST */
|
||||
} data;
|
||||
int arg_count; /* number of arguments (in data.list) */
|
||||
|
||||
/* ------ misc context information ---------------------------------------------------------- */
|
||||
configfile_t *configfile;
|
||||
context_t *context;
|
||||
};
|
||||
|
||||
/* ------ dotconf_create() - create the configfile_t needed for further dot.conf fun ------------ */
|
||||
configfile_t *dotconf_create(char *, const configoption_t *, context_t *, unsigned long);
|
||||
|
||||
/* ------ dotconf_cleanup() - tidy up behind dotconf_create and the parser dust ----------------- */
|
||||
void dotconf_cleanup(configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_command_loop() - iterate through each line of file and handle the commands ---- */
|
||||
int dotconf_command_loop(configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_command_loop_until_error() - like continue_line but return on the first error - */
|
||||
const char *dotconf_command_loop_until_error(configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_continue_line() - check if line continuation is to be handled ----------------- */
|
||||
int dotconf_continue_line(char *buffer, size_t length);
|
||||
|
||||
/* ------ dotconf_get_next_line() - read in the next line of the configfile_t ------------------- */
|
||||
int dotconf_get_next_line(char *buffer, size_t bufsize, configfile_t *configfile);
|
||||
|
||||
/* ------ dotconf_get_here_document() - read the here document until delimit is found ----------- */
|
||||
char *dotconf_get_here_document(configfile_t *configfile, const char *delimit);
|
||||
|
||||
/* ------ dotconf_invoke_command() - call the callback for command_t ---------------------------- */
|
||||
const char *dotconf_invoke_command(configfile_t *configfile, command_t *cmd);
|
||||
|
||||
/* ------ dotconf_find_command() - iterate through all registered options trying to match ------- */
|
||||
configoption_t *dotconf_find_command(configfile_t *configfile, const char *command);
|
||||
|
||||
/* ------ dotconf_read_arg() - read one argument from the line handling quoting and escaping ---- */
|
||||
/*
|
||||
side effects: the char* returned by dotconf_read_arg is malloc() before, hence that pointer
|
||||
will have to be free()ed later.
|
||||
*/
|
||||
char *dotconf_read_arg(configfile_t *configfile, signed char **line);
|
||||
|
||||
/* ------ dotconf_handle_command() - parse, substitute, find, invoke the command found in buffer */
|
||||
const char *dotconf_handle_command(configfile_t *configfile, char *buffer);
|
||||
|
||||
/* ------ dotconf_register_option() - add a new option table to the list of commands ------------ */
|
||||
void dotconf_register_options(configfile_t *configfile, const configoption_t *options);
|
||||
|
||||
/* ------ dotconf_warning() - handle the dispatch of error messages of various levels ----------- */
|
||||
int dotconf_warning(configfile_t *configfile, int level, unsigned long errnum, const char *, ...);
|
||||
|
||||
/* ------ dotconf_callback() - register a special callback -------------------------------------- */
|
||||
void dotconf_callback(configfile_t *configfile, callback_types type, dotconf_callback_t);
|
||||
|
||||
/* ------ dotconf_substitute_env() - handle the substitution on environment variables ----------- */
|
||||
char *dotconf_substitute_env(configfile_t *, char *);
|
||||
|
||||
/* ------ internal utility function that verifies if a character is in the WILDCARDS list -- */
|
||||
int dotconf_is_wild_card(char value);
|
||||
|
||||
/* ------ internal utility function that calls the appropriate routine for the wildcard passed in -- */
|
||||
int dotconf_handle_wild_card(command_t* cmd, char wild_card, char* path, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that frees allocated memory from dotcont_find_wild_card -- */
|
||||
void dotconf_wild_card_cleanup(char* path, char* pre);
|
||||
|
||||
/* ------ internal utility function to check for wild cards in file path -- */
|
||||
/* ------ path and pre must be freed by the developer ( dotconf_wild_card_cleanup) -- */
|
||||
int dotconf_find_wild_card(char* filename, char* wildcard, char** path, char** pre, char** ext);
|
||||
|
||||
/* ------ internal utility function that compares two stings from back to front -- */
|
||||
int dotconf_strcmp_from_back(const char* s1, const char* s2);
|
||||
|
||||
/* ------ internal utility function that determins if a string matches the '?' criteria -- */
|
||||
int dotconf_question_mark_match(char* dir_name, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that determins if a string matches the '*' criteria -- */
|
||||
int dotconf_star_match(char* dir_name, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that determins matches for filenames with -- */
|
||||
/* ------ a '?' in name and calls the Internal Include function on that filename -- */
|
||||
int dotconf_handle_question_mark(command_t* cmd, char* path, char* pre, char* ext);
|
||||
|
||||
/* ------ internal utility function that determins matches for filenames with -- */
|
||||
/* ------ a '*' in name and calls the Internal Include function on that filename -- */
|
||||
int dotconf_handle_star(command_t* cmd, char* path, char* pre, char* ext);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* DOTCONF_H */
|
||||
BIN
code/tags/sys_nicmonitor-1.8/test/getmem
Executable file
BIN
code/tags/sys_nicmonitor-1.8/test/getmem
Executable file
Binary file not shown.
82
code/tags/sys_nicmonitor-1.8/test/getmem.c
Normal file
82
code/tags/sys_nicmonitor-1.8/test/getmem.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nicinfo_shm.h"
|
||||
|
||||
typedef struct net_info{
|
||||
NETCARD_INFO info[MAXNICNUM];
|
||||
}SHM;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret;
|
||||
NETCARD_INFO net;
|
||||
#if 0
|
||||
char shm_path[1024];
|
||||
char sem_path[1024];
|
||||
|
||||
memset(shm_path, 0, sizeof(shm_path));
|
||||
memset(sem_path, 0, sizeof(sem_path));
|
||||
|
||||
strcpy(shm_path, getenv("D5000_HOME"));
|
||||
strcat(shm_path, "/conf/nic/shm_sys_netcard_info");
|
||||
strcpy(sem_path, getenv("D5000_HOME"));
|
||||
strcat(sem_path, "/conf/nic/sem_sys_netcard_info");
|
||||
if((ret = init_nic_info(shm_path, sem_path)) == -1){
|
||||
fprintf(stderr, "shm or sem path error!\n");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
memset(&net, 0, sizeof(NETCARD_INFO));
|
||||
if(argc != 2){
|
||||
fprintf(stderr, "Usage:%s netcard\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
while((ret = get_nic_info(argv[1], &net)) != -1){
|
||||
fprintf(stdout, "==========================网卡信息==============================\n");
|
||||
fprintf(stdout,"DEVICE NAME : %s\nIPADDR : %d.%d.%d.%d\nBROADCAST : %d.%d.%d.%d\nNETMASK : %d.%d.%d.%d\nHWADDR : %02x:%02x:%02x:%02x:%02x:%02x\nFLAGS = %d\nMTU = %d\nTx_queue_len = %d\nTime_stamp = %d\n\nReceive bytes:%llu\nReceive packets:%llu\nReceive errors:%lu\nReceive dropped:%lu\nReceive multicast:%lu\nReceive fifo:%lu\n\nTransmit bytes:%llu\nTransmit packets:%llu\nTransmit errors:%lu\nTransmit dropped:%lu\nTransmit fifo:%lu\nTransmit collisions:%lu\nTransmit carrier:%lu\n\nAverage flow:%d\n",
|
||||
net.charname,
|
||||
(unsigned char)net.addr.sa_data[2],
|
||||
(unsigned char)net.addr.sa_data[3],
|
||||
(unsigned char)net.addr.sa_data[4],
|
||||
(unsigned char)net.addr.sa_data[5],
|
||||
(unsigned char)net.broadaddr.sa_data[2],
|
||||
(unsigned char)net.broadaddr.sa_data[3],
|
||||
(unsigned char)net.broadaddr.sa_data[4],
|
||||
(unsigned char)net.broadaddr.sa_data[5],
|
||||
(unsigned char)net.netmask.sa_data[2],
|
||||
(unsigned char)net.netmask.sa_data[3],
|
||||
(unsigned char)net.netmask.sa_data[4],
|
||||
(unsigned char)net.netmask.sa_data[5],
|
||||
(unsigned char)net.hwaddr.sa_data[0],
|
||||
(unsigned char)net.hwaddr.sa_data[1],
|
||||
(unsigned char)net.hwaddr.sa_data[2],
|
||||
(unsigned char)net.hwaddr.sa_data[3],
|
||||
(unsigned char)net.hwaddr.sa_data[4],
|
||||
(unsigned char)net.hwaddr.sa_data[5],
|
||||
net.flags,
|
||||
net.mtu,
|
||||
net.tx_queue_len,
|
||||
net.time_stamp,
|
||||
net.rx_bytes,
|
||||
net.rx_packets,
|
||||
net.rx_errors,
|
||||
net.rx_dropped,
|
||||
net.rx_multicast,
|
||||
net.rx_fifo_errors,
|
||||
net.tx_bytes,
|
||||
net.tx_packets,
|
||||
net.tx_errors,
|
||||
net.tx_dropped,
|
||||
net.tx_fifo_errors,
|
||||
net.collisions,
|
||||
net.tx_carrier_errors,
|
||||
net.average_flow);
|
||||
getchar();
|
||||
}
|
||||
fprintf(stderr, "No information of %s !\n", argv[1]);
|
||||
|
||||
// rls_nic_info();
|
||||
return 0;
|
||||
}
|
||||
66
code/tags/sys_nicmonitor-1.8/test/mnic.h
Normal file
66
code/tags/sys_nicmonitor-1.8/test/mnic.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef __MNIC_H
|
||||
#define __MNIC_H
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <time.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/param.h>
|
||||
#include <linux/types.h>
|
||||
#include <glob.h>
|
||||
#include <linux/sockios.h>
|
||||
#include <linux/ethtool.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "sys_netcard.h"
|
||||
#include "dotconf.h"
|
||||
|
||||
#define IPSIZE 16
|
||||
#define PATH_SIZE 256
|
||||
|
||||
DOTCONF_CB(cb_str);
|
||||
DOTCONF_CB(cb_int);
|
||||
|
||||
typedef struct __config_file_st{
|
||||
unsigned char domain;
|
||||
short serv;
|
||||
short event;
|
||||
int udpport;
|
||||
char sys_netcard_shm_path[PATH_SIZE];
|
||||
char sys_netcard_sem_path[PATH_SIZE];
|
||||
int monitor_interval;
|
||||
int write_interval;
|
||||
int flow_interval;
|
||||
int flow_limit;
|
||||
int flow_peak;
|
||||
char udp[NIC_NAME_LEN];
|
||||
char nic[MAXNICNUM][NIC_NAME_LEN];
|
||||
char ip[IPSIZE] ;
|
||||
}CONFIG_FILE_ST;
|
||||
|
||||
|
||||
char *get_name(char *, char *);
|
||||
int if_fetch(NETCARD_INFO *);
|
||||
int ioc_get_name(NETCARD_INFO *);
|
||||
int get_dev_fields(char *p, NETCARD_INFO *);
|
||||
void send_alarm(D5000_NIC_ALARM *, int , char *, char *);
|
||||
void record_log(void);
|
||||
|
||||
#endif
|
||||
18
code/tags/sys_nicmonitor-1.8/test/nicinfo_shm.h
Normal file
18
code/tags/sys_nicmonitor-1.8/test/nicinfo_shm.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef NIC_SHM_H
|
||||
#define NIC_SHM_H
|
||||
|
||||
#include "sys_netcard.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int init_nic_info(char *share_path, char *sem_path);
|
||||
int get_nic_info(char *nic_name, NETCARD_INFO *net_info);
|
||||
void rls_nic_info(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
BIN
code/tags/sys_nicmonitor-1.8/test/read_shm
Executable file
BIN
code/tags/sys_nicmonitor-1.8/test/read_shm
Executable file
Binary file not shown.
127
code/tags/sys_nicmonitor-1.8/test/read_shm.c
Normal file
127
code/tags/sys_nicmonitor-1.8/test/read_shm.c
Normal file
@@ -0,0 +1,127 @@
|
||||
#include "mnic.h"
|
||||
|
||||
typedef struct net_info{
|
||||
NETCARD_INFO info[MAXNICNUM];
|
||||
}SHM;
|
||||
|
||||
void get_sem(int semid)
|
||||
{
|
||||
struct sembuf lock;
|
||||
|
||||
lock.sem_num = 0;
|
||||
lock.sem_op = -1;
|
||||
lock.sem_flg = SEM_UNDO;
|
||||
|
||||
while(semop(semid, &lock, 1)){
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
perror("semop()");
|
||||
}
|
||||
}
|
||||
|
||||
void release_sem(int semid)
|
||||
{
|
||||
int ret;
|
||||
struct sembuf unlock;
|
||||
|
||||
unlock.sem_num = 0;
|
||||
unlock.sem_op = 1;
|
||||
unlock.sem_flg = SEM_UNDO;
|
||||
|
||||
if((ret = semop(semid, &unlock, 1)) == -1){
|
||||
perror("semop()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fd = -1;
|
||||
int i;
|
||||
int semid;
|
||||
key_t key;
|
||||
SHM *p;
|
||||
struct timespec s_time, os_time;
|
||||
char shm_path[1024];
|
||||
char sem_path[1024];
|
||||
|
||||
memset(shm_path, 0, sizeof(shm_path));
|
||||
memset(sem_path, 0, sizeof(sem_path));
|
||||
|
||||
strcpy(shm_path, "/tmp/sys_netcard_shm_path");
|
||||
strcpy(sem_path, "/tmp/sys_netcard_sem_path");
|
||||
|
||||
s_time.tv_sec = 1;
|
||||
s_time.tv_nsec = 0;
|
||||
|
||||
if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) {
|
||||
perror("ftok()");
|
||||
exit(1);
|
||||
}
|
||||
if((semid = semget(key, 1, IPC_CREAT)) == -1){
|
||||
fprintf(stderr, "Create semaphore error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if((fd = open(shm_path, O_RDWR)) == -1){
|
||||
fprintf(stderr, "open():%s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
p = mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0);
|
||||
if(p == MAP_FAILED){
|
||||
perror("mmap");
|
||||
return -1;
|
||||
}
|
||||
perror("mmap");
|
||||
while(1){
|
||||
get_sem(semid);
|
||||
for(i = 0; p->info[i].charname[0] != 0; i++){
|
||||
fprintf(stdout, "==========================网卡信息 [%d]==============================\n", (i + 1));
|
||||
fprintf(stdout,"KEYID = %ld\nBONDING:%s\nDEVICE NAME : %s\nIPADDR : %d.%d.%d.%d\nBROADCAST : %d.%d.%d.%d\nNETMASK : %d.%d.%d.%d\nHWADDR : %02x:%02x:%02x:%02x:%02x:%02x\nFLAGS = %d\nMTU = %d\nTx_queue_len = %d\nTime_stamp = %d\n\nReceive bytes:%llu\nReceive packets:%llu\nReceive errors:%lu\nReceive dropped:%lu\nReceive multicast:%lu\nReceive fifo:%lu\n\nTransmit bytes:%llu\nTransmit packets:%llu\nTransmit errors:%lu\nTransmit dropped:%lu\nTransmit fifo:%lu\nTransmit collisions:%lu\nTransmit carrier:%lu\nAverage flow : %lld\n",
|
||||
p->info[i].ID,
|
||||
p->info[i].descr,
|
||||
p->info[i].charname,
|
||||
(unsigned char)p->info[i].addr.sa_data[2],
|
||||
(unsigned char)p->info[i].addr.sa_data[3],
|
||||
(unsigned char)p->info[i].addr.sa_data[4],
|
||||
(unsigned char)p->info[i].addr.sa_data[5],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[2],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[3],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[4],
|
||||
(unsigned char)p->info[i].broadaddr.sa_data[5],
|
||||
(unsigned char)p->info[i].netmask.sa_data[2],
|
||||
(unsigned char)p->info[i].netmask.sa_data[3],
|
||||
(unsigned char)p->info[i].netmask.sa_data[4],
|
||||
(unsigned char)p->info[i].netmask.sa_data[5],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[0],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[1],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[2],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[3],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[4],
|
||||
(unsigned char)p->info[i].hwaddr.sa_data[5],
|
||||
p->info[i].flags,
|
||||
p->info[i].mtu,
|
||||
p->info[i].tx_queue_len,
|
||||
p->info[i].time_stamp,
|
||||
p->info[i].rx_bytes,
|
||||
p->info[i].rx_packets,
|
||||
p->info[i].rx_errors,
|
||||
p->info[i].rx_dropped,
|
||||
p->info[i].rx_multicast,
|
||||
p->info[i].rx_fifo_errors,
|
||||
p->info[i].tx_bytes,
|
||||
p->info[i].tx_packets,
|
||||
p->info[i].tx_errors,
|
||||
p->info[i].tx_dropped,
|
||||
p->info[i].tx_fifo_errors,
|
||||
p->info[i].collisions,
|
||||
p->info[i].tx_carrier_errors,
|
||||
p->info[i].average_flow);
|
||||
}
|
||||
nanosleep(&s_time, &os_time);
|
||||
release_sem(semid);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
BIN
code/tags/sys_nicmonitor-1.8/test/recv_msg
Executable file
BIN
code/tags/sys_nicmonitor-1.8/test/recv_msg
Executable file
Binary file not shown.
72
code/tags/sys_nicmonitor-1.8/test/recv_msg.c
Normal file
72
code/tags/sys_nicmonitor-1.8/test/recv_msg.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "mnic.h"
|
||||
#define IP4STRSIZE 16
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int ret, value;
|
||||
int sock_sd, sock_port = 15000;
|
||||
D5000_NIC_ALARM mesg;
|
||||
struct sockaddr_in myend, hisend;
|
||||
socklen_t hisend_len;
|
||||
fd_set rset;
|
||||
char ip4str[IP4STRSIZE];
|
||||
|
||||
if(argc > 1){
|
||||
sock_port = atoi(argv[1]);
|
||||
if(sock_port <= 1024 || sock_port > 65535){
|
||||
fprintf(stderr, "Invalid socket port!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (sock_sd==-1) {
|
||||
perror("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
value=1;
|
||||
ret = setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value));
|
||||
if (ret==-1) {
|
||||
perror("setsockopt()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
myend.sin_family = AF_INET;
|
||||
myend.sin_port = htons(sock_port);
|
||||
inet_pton(AF_INET, "0.0.0.0", &myend.sin_addr);
|
||||
|
||||
ret = bind(sock_sd, (struct sockaddr*)&myend, sizeof(myend));
|
||||
if (ret==-1) {
|
||||
perror("bind()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
hisend_len = sizeof(hisend);
|
||||
while (1) {
|
||||
FD_ZERO(&rset);
|
||||
FD_SET(sock_sd, &rset);
|
||||
ret = select(sock_sd+1, &rset, NULL, NULL, NULL);
|
||||
if (ret==-1) {
|
||||
perror("select()");
|
||||
continue;
|
||||
}
|
||||
ret = recvfrom(sock_sd, &mesg, sizeof(mesg), 0, (struct sockaddr*)&hisend, &hisend_len);
|
||||
inet_ntop(AF_INET, &hisend.sin_addr, ip4str, IP4STRSIZE);
|
||||
fprintf(stdout, "===========Recieved from %s============\n", ip4str);
|
||||
fprintf(stdout, "message length : %d\nsend sequence : %d\nservices ID : %d\nenevt ID : %d\ndomain ID : %d\n", mesg.tMsgFrame.len, mesg.tMsgFrame.seqno, mesg.tMsgFrame.serv, mesg.tMsgFrame.event, mesg.tMsgFrame.domain);
|
||||
if(mesg.tSysNetcardAlarm.flags == NETCARD_ALARM_SWITCH)
|
||||
fprintf(stdout, "切换网卡名: %s\n", mesg.tSysNetcardAlarm.switch_devname);
|
||||
else if(mesg.tSysNetcardAlarm.flags == NETCARD_ALARM_RESUME)
|
||||
fprintf(stdout, "恢复网卡名: %s\n", mesg.tSysNetcardAlarm.fault_devname);
|
||||
else
|
||||
fprintf(stdout, "故障网卡名: %s\n", mesg.tSysNetcardAlarm.fault_devname);
|
||||
fprintf(stdout, "状态标记: %d\n", mesg.tSysNetcardAlarm.flags);
|
||||
fprintf(stdout, "重发次数: %d\n", mesg.tSysNetcardAlarm.retrytimes);
|
||||
}
|
||||
close(sock_sd);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
87
code/tags/sys_nicmonitor-1.8/test/sys_netcard.h
Normal file
87
code/tags/sys_nicmonitor-1.8/test/sys_netcard.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#ifndef _SYS_NETCARD_H
|
||||
#define _SYS_NETCARD_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifndef NIC_NAME_LEN
|
||||
#define NIC_NAME_LEN 64
|
||||
#endif
|
||||
|
||||
#define SEM_PROJ_ID 's'
|
||||
#define MAXNICNUM 32
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
|
||||
typedef long KEYID;
|
||||
|
||||
typedef struct _msg_frame // message frame
|
||||
{
|
||||
short len; // message length
|
||||
short seqno; // send sequence
|
||||
short serv; // services ID
|
||||
short event; // event ID
|
||||
unsigned char domain; // domain ID
|
||||
unsigned char ctxt; // Context ID
|
||||
short stid; // SOURCE task id
|
||||
short dtid; // DESTINATION task ID
|
||||
unsigned char ver_coding; // 版本号 + 编码
|
||||
unsigned char mes_type; // 帧类型
|
||||
}MSG_FRAME, *LPMSG_FRAME;
|
||||
|
||||
typedef struct _net_info
|
||||
{
|
||||
KEYID ID;
|
||||
char charname[NIC_NAME_LEN];
|
||||
char descr[128];
|
||||
struct sockaddr addr;
|
||||
struct sockaddr broadaddr;
|
||||
struct sockaddr netmask;
|
||||
struct sockaddr hwaddr;
|
||||
int time_stamp;
|
||||
short flags;
|
||||
int mtu;
|
||||
int tx_queue_len;
|
||||
int average_flow;
|
||||
unsigned long long rx_packets;
|
||||
unsigned long long tx_packets;
|
||||
unsigned long long rx_bytes;
|
||||
unsigned long long tx_bytes;
|
||||
unsigned long rx_errors;
|
||||
unsigned long tx_errors;
|
||||
unsigned long rx_dropped;
|
||||
unsigned long tx_dropped;
|
||||
unsigned long rx_multicast;
|
||||
unsigned long collisions;
|
||||
unsigned long rx_fifo_errors;
|
||||
unsigned long tx_carrier_errors;
|
||||
unsigned long tx_fifo_errors;
|
||||
}NETCARD_INFO, *LPNETCARD_INFO;
|
||||
|
||||
#define NETCARD_ALARM_FAULT 1
|
||||
#define NETCARD_ALARM_RESUME 2
|
||||
#define NETCARD_ALARM_SWITCH 3
|
||||
#define NETCARD_ALARM_ABNORMAL 4
|
||||
typedef struct _sys_netcard_alarm
|
||||
{
|
||||
char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */
|
||||
char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */
|
||||
short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/
|
||||
short retrytimes; //0,1,2 重发次数
|
||||
}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM;
|
||||
|
||||
|
||||
typedef struct _d5000_nic_alarm
|
||||
{
|
||||
MSG_FRAME tMsgFrame ;
|
||||
SYS_NETCARD_ALARM tSysNetcardAlarm ;
|
||||
}D5000_NIC_ALARM, *LPD5000_NIC_ALARM;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
39
code/trunk/README
Normal file
39
code/trunk/README
Normal file
@@ -0,0 +1,39 @@
|
||||
--V1.8 --
|
||||
2013-05-27
|
||||
对系统高磁盘IO情况下,应用如果进行IO将可能会被阻塞。本版本为此作出调整。
|
||||
cs-fes1上编译测试OK。
|
||||
|
||||
1) 去掉shm和sem的实体文件
|
||||
#define SHM_PATH "$D5000_HOME/share/sys_netcard_shm_path"
|
||||
#define SEM_PATH "$D5000_HOME/share/sys_netcard_sem_path"
|
||||
|
||||
用key=0x1d5200 代替 sem
|
||||
用key=0x1d5010 代替 shm
|
||||
|
||||
|
||||
2)去掉动态库中get_nic_info接口中,判断路径的opendir调用,采用实际路径
|
||||
上层应用每秒可能调用3次这个接口,每判断一个网卡都调用一次,判断都个网卡的状态时就调用多次。
|
||||
|
||||
可改进:一次调用get_nic_info接口,可输入多个网卡,一次返回所有网卡状态信息。
|
||||
|
||||
3) record_log函数为日志函数,每次写一次,都重新打开一次文件。
|
||||
修改为线程模式,当记录日志时,不会被磁盘IO阻塞。
|
||||
|
||||
----
|
||||
|
||||
|
||||
V1.6与V1.7差异
|
||||
共享内存路径修改:
|
||||
由/home/d5000/someone/tmp/sys_netcard_shm_path 改为 /home/d5000/someone/share/sys_netcard_shm_path
|
||||
信号量路径修改:
|
||||
由/home/d5000/someone/tmp/sys_netcard_sem_path 改为 /home/d5000/someone/share/sys_netcard_sem_path
|
||||
|
||||
注意:在编译过程中,需要动态看libman.so,它存放在目录 tags/下面 ,在进行编译的时候需要将其拷贝到
|
||||
/lib64/目录下,编译的基础环境是 Rocky 4.2 系统。
|
||||
|
||||
pkgmk命令的使用:
|
||||
将源代码打包成name-version.tar.gz样式,编写Pkgfile文件。
|
||||
使用命令:
|
||||
pkgmk -um #更新软件包的md5值
|
||||
pkgmk -kw #制作pkg安装包
|
||||
|
||||
BIN
code/trunk/doc/网卡监视程序动态库使用手册【新】.doc
Normal file
BIN
code/trunk/doc/网卡监视程序动态库使用手册【新】.doc
Normal file
Binary file not shown.
BIN
code/trunk/doc/网卡监视程序安装说明以及使用手册【新】.doc
Normal file
BIN
code/trunk/doc/网卡监视程序安装说明以及使用手册【新】.doc
Normal file
Binary file not shown.
@@ -1,7 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
mv /usr/lib64/libnic_shm.so ~d5000/lib
|
||||
mv /usr/bin/sys_nicmonitor ~d5000/bin
|
||||
|
||||
chown d5000.d5000 ~d5000/lib/libnic_shm.so
|
||||
chown d5000.d5000 ~d5000/bin/sys_nicmonitor
|
||||
20
code/trunk/src/Pkgfile
Normal file
20
code/trunk/src/Pkgfile
Normal file
@@ -0,0 +1,20 @@
|
||||
# Description: A system to store and display time-series data.
|
||||
# URL: http://oss.oetiker.ch/rrdtool/index.en.html
|
||||
# Maintainer: Danny Rawlins, monster dot romster at gmail dot com
|
||||
# Packager: Younes Hafri, ycrux at club-internet dot fr
|
||||
# Depends on: libart_lgpl libcgi libgd pango python tcl intltool
|
||||
|
||||
name=sys_nicmonitor
|
||||
version=1.8
|
||||
release=x86_64-Linx-Rocky4.2
|
||||
source=/home/d5000/d5000/linx/1.8/$name-$version.tar.gz
|
||||
|
||||
build() {
|
||||
cd $name-$version
|
||||
|
||||
make
|
||||
make DESTDIR=$PKG install
|
||||
# mkdir -p $PKG/var/log/netcard
|
||||
# chmod 777 $PKG/var/log/netcard
|
||||
}
|
||||
|
||||
65
code/trunk/src/README1.8
Normal file
65
code/trunk/src/README1.8
Normal file
@@ -0,0 +1,65 @@
|
||||
=========
|
||||
编译说明:
|
||||
在Rocky 4.2上编译
|
||||
1)修改Pkgfile中的source指向的源文件位置
|
||||
2)然后执行: sh build.sh
|
||||
将在当前的目录下生成安装包:
|
||||
sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
|
||||
========
|
||||
安装说明:
|
||||
1)安装,以root的用户执行安装命令:
|
||||
pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装
|
||||
卸载命令: pkgrm sys_nicmonitor
|
||||
更新方式安装命令:
|
||||
pkgadd -u sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
|
||||
|
||||
2)查看安装是否成功
|
||||
pkginfo -i|grep sys_nicmonitor
|
||||
或者
|
||||
查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否
|
||||
|
||||
========
|
||||
版本说明:
|
||||
--V1.8 --
|
||||
2013-05-27
|
||||
对系统高磁盘IO情况下,应用如果进行IO将可能会被阻塞。本版本为此作出调整。
|
||||
cs-fes1上编译测试OK。
|
||||
|
||||
1) 去掉shm和sem的实体文件
|
||||
#define SHM_PATH "$D5000_HOME/share/sys_netcard_shm_path"
|
||||
#define SEM_PATH "$D5000_HOME/share/sys_netcard_sem_path"
|
||||
|
||||
用key=0x1d5200 代替 sem
|
||||
用key=0x1d5010 代替 shm
|
||||
|
||||
|
||||
2)去掉动态库中get_nic_info接口中,判断路径的opendir调用,采用实际路径
|
||||
上层应用每秒可能调用3次这个接口,每判断一个网卡都调用一次,判断都个网卡的状态时就调用多次。
|
||||
|
||||
可改进:一次调用get_nic_info接口,可输入多个网卡,一次返回所有网卡状态信息。
|
||||
|
||||
3) record_log函数为日志函数,每次写一次,都重新打开一次文件。
|
||||
修改为线程模式,当记录日志时,不会被磁盘IO阻塞。
|
||||
|
||||
4) 安装目录为d5000用户的HOME目录下的lib和bin目录
|
||||
|
||||
----
|
||||
|
||||
|
||||
V1.6与V1.7差异
|
||||
共享内存路径修改:
|
||||
由/home/d5000/someone/tmp/sys_netcard_shm_path 改为 /home/d5000/someone/share/sys_netcard_shm_path
|
||||
信号量路径修改:
|
||||
由/home/d5000/someone/tmp/sys_netcard_sem_path 改为 /home/d5000/someone/share/sys_netcard_sem_path
|
||||
|
||||
注意:在编译过程中,需要动态看libman.so,它存放在目录 tags/下面 ,在进行编译的时候需要将其拷贝到
|
||||
/lib64/目录下,编译的基础环境是 Rocky 4.2 系统。
|
||||
|
||||
pkgmk命令的使用:
|
||||
将源代码打包成name-version.tar.gz样式,编写Pkgfile文件。
|
||||
使用命令:
|
||||
pkgmk -um #更新软件包的md5值
|
||||
pkgmk -kw #制作pkg安装包
|
||||
|
||||
0
code/trunk/src/V1.8
Normal file
0
code/trunk/src/V1.8
Normal file
7
code/trunk/src/build.sh
Normal file
7
code/trunk/src/build.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
pkgmk -um
|
||||
|
||||
#For debug
|
||||
#pkgmk -kw -pa post_add.sh
|
||||
|
||||
#For release
|
||||
pkgmk -pa post_add.sh
|
||||
16
code/trunk/src/post_add.sh
Normal file
16
code/trunk/src/post_add.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
#echo $HOME
|
||||
DFDIR=`cat /etc/passwd | grep d5000 | cut -d ":" -f 6 `
|
||||
#LOGDIR=$DFDIR/var/log/netcard
|
||||
#mkdir -p $LOGDIR
|
||||
echo $DFDIR
|
||||
#echo $LOGDIR
|
||||
|
||||
echo "$DFDIR/lib/libnic_shm.so"
|
||||
echo "$DFDIR/bin/sys_nicmonitor"
|
||||
|
||||
mv /usr/lib64/libnic_shm.so $DFDIR/lib
|
||||
mv /usr/bin/sys_nicmonitor $DFDIR/bin
|
||||
|
||||
chown d5000.d5000 $DFDIR/lib/libnic_shm.so
|
||||
chown d5000.d5000 $DFDIR/bin/sys_nicmonitor
|
||||
10
code/trunk/src/post_mk.sh
Normal file
10
code/trunk/src/post_mk.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
PWD=`pwd`
|
||||
#pkg=`basename $PWD`
|
||||
pkg=sys_nicmonitor
|
||||
|
||||
# install the script which is run just after pkgadd or
|
||||
# when booting from disk first time
|
||||
mkdir -p $PKG/$PI_DIR
|
||||
install -m 755 post_add.sh $PKG/$PI_DIR/${pkg}.post_add
|
||||
BIN
code/trunk/src/sys_nicmonitor-1.8.tar.gz
Normal file
BIN
code/trunk/src/sys_nicmonitor-1.8.tar.gz
Normal file
Binary file not shown.
@@ -9,7 +9,7 @@ all:sys_nicmonitor libnic_shm.so
|
||||
CFLAGS += -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral #-g
|
||||
|
||||
sys_nicmonitor:mnic.o read_netcard.o send_alarm.o
|
||||
$(CC) -ldotconf -lpthread -o $@ $^
|
||||
$(CC) -lman -ldotconf -lpthread -o $@ $^
|
||||
libnic_shm.so:nicinfo_shm.c
|
||||
$(CC) -fpic -shared -o $@ $^
|
||||
clean:
|
||||
|
||||
BIN
code/trunk/sys_nicmonitor/bin/sys_nicmonitor
Executable file
BIN
code/trunk/sys_nicmonitor/bin/sys_nicmonitor
Executable file
Binary file not shown.
@@ -2,6 +2,9 @@
|
||||
#include "dotconf.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <pthread.h>
|
||||
#include "proc_inv.h"
|
||||
|
||||
#define THRNR 10
|
||||
@@ -10,14 +13,14 @@
|
||||
#define HOST_NAME_SIZE 40
|
||||
#define BOND_PATH "/proc/net/bonding/*"
|
||||
#define LOG_PATH "/var/log/netcard/"
|
||||
#define SHM_PATH "/tmp/sys_netcard_shm_path"
|
||||
#define SEM_PATH "/tmp/sys_netcard_sem_path"
|
||||
#define SHM_PATH "/share/sys_netcard_shm_path"
|
||||
#define SEM_PATH "/share/sys_netcard_sem_path"
|
||||
#define MAX_GW 32
|
||||
#define DEV_MAXLEN 32
|
||||
#define IPLEN 16
|
||||
#define CONF_FILE "/conf/nic/sys_netcard_conf.txt"
|
||||
|
||||
#define MNIC_VERSION "1.6"
|
||||
#define MNIC_VERSION "1.8"
|
||||
|
||||
#define NIC_UNKNOWN 0
|
||||
#define NIC_DOWN 1
|
||||
@@ -33,16 +36,16 @@ typedef struct __nic_dev{
|
||||
}NIC_DEV;
|
||||
|
||||
typedef struct __config_file_st{
|
||||
unsigned char domain;
|
||||
short serv;
|
||||
short event;
|
||||
int udpport;
|
||||
int monitor_interval;
|
||||
int write_interval;
|
||||
int flow_interval;
|
||||
int flow_limit;
|
||||
int flow_peak;
|
||||
char udp[NIC_NAME_LEN];
|
||||
unsigned char domain;
|
||||
short serv;
|
||||
short event;
|
||||
int udpport;
|
||||
int monitor_interval;
|
||||
int write_interval;
|
||||
int flow_interval;
|
||||
int flow_limit;
|
||||
int flow_peak;
|
||||
char udp[NIC_NAME_LEN];
|
||||
NIC_DEV nic[MAXNICNUM]; //被监视网卡的列表
|
||||
char ip[IPSIZE];
|
||||
int pingnum; //OPTIONAL!! How many times for once ping check? Default is 2.
|
||||
@@ -112,49 +115,78 @@ int nicnum = 0;
|
||||
CONFIG_FILE_ST conf;
|
||||
|
||||
char myconf[][64] = {
|
||||
"domain", // 0
|
||||
"serv", // 1
|
||||
"event", // 2
|
||||
"udpport", // 3
|
||||
"sys_netcard_shm_path", // 4
|
||||
"sys_netcard_sem_path", // 5
|
||||
"monitor_interval", // 6
|
||||
"write_interval", // 7
|
||||
"flow_interval", // 8
|
||||
"flow_limit", // 9
|
||||
"flow_peak", // 10
|
||||
"udp", // 11
|
||||
"nic", // 12
|
||||
"ping", // 13
|
||||
"domain", // 0
|
||||
"serv", // 1
|
||||
"event", // 2
|
||||
"udpport", // 3
|
||||
"sys_netcard_shm_path", // 4
|
||||
"sys_netcard_sem_path", // 5
|
||||
"monitor_interval", // 6
|
||||
"write_interval", // 7
|
||||
"flow_interval", // 8
|
||||
"flow_limit", // 9
|
||||
"flow_peak", // 10
|
||||
"udp", // 11
|
||||
"nic", // 12
|
||||
"ping", // 13
|
||||
"pingnum", // 14
|
||||
"pinglap", // 15
|
||||
""
|
||||
""
|
||||
};
|
||||
|
||||
static const configoption_t options[] = {
|
||||
{myconf[0], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[1], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[2], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[3], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[4], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[5], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[6], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[7], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[8], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[9], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[10], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[11], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[12], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[13], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[14], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[15], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
LAST_OPTION
|
||||
{myconf[0], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[1], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[2], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[3], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[4], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[5], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[6], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[7], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[8], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[9], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[10], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[11], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[12], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[13], ARG_STR, cb_str, NULL, CTX_ALL},
|
||||
{myconf[14], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
{myconf[15], ARG_INT, cb_int, NULL, CTX_ALL},
|
||||
LAST_OPTION
|
||||
};
|
||||
/****dotconf****/
|
||||
|
||||
void create_dir(char *create_path);
|
||||
|
||||
static pthread_mutex_t lock_record_log = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define LOG_LOCK pthread_mutex_lock(&lock_record_log)
|
||||
#define LOG_UNLOCK pthread_mutex_unlock(&lock_record_log)
|
||||
//static int is_checkdir = 0;
|
||||
static void *record_log_thread( void *arg )
|
||||
{
|
||||
char *log_str = (char *)arg;
|
||||
FILE *log_fp = NULL;
|
||||
|
||||
LOG_LOCK;
|
||||
// if( !is_checkdir ) {
|
||||
create_dir( log_path );
|
||||
// is_checkdir = 1;
|
||||
// }
|
||||
if((log_fp = fopen(log_path, "a")) != NULL){
|
||||
if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){
|
||||
}
|
||||
fclose(log_fp);
|
||||
}
|
||||
|
||||
LOG_UNLOCK;
|
||||
free(log_str);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* write error in logfile */
|
||||
void record_log(char *str)
|
||||
{
|
||||
#if 0
|
||||
time_t tamp;
|
||||
char str_tm[4];
|
||||
char log_str[512];
|
||||
@@ -181,21 +213,62 @@ void record_log(char *str)
|
||||
if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){
|
||||
}
|
||||
fclose(log_fp);
|
||||
}
|
||||
}
|
||||
#else
|
||||
time_t tamp;
|
||||
char str_tm[4];
|
||||
char log_str[512];
|
||||
// FILE *log_fp = NULL;
|
||||
struct tm tmptr;
|
||||
struct timeval tv;
|
||||
struct timezone tz;
|
||||
|
||||
// if((log_fp = fopen(log_path, "a")) != NULL){
|
||||
tamp = time(NULL);
|
||||
memset(str_tm, 0, sizeof(str_tm));
|
||||
memset(log_str, 0, sizeof(log_str));
|
||||
localtime_r(&tamp, &tmptr);
|
||||
gettimeofday(&tv, &tz);
|
||||
snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000);
|
||||
if(str_tm[1] == '\0')str_tm[1] = '0';
|
||||
if(str_tm[2] == '\0')str_tm[2] = '0';
|
||||
strftime(log_str, sizeof(log_str), "%F %T.", &tmptr);
|
||||
strcat(log_str, str_tm);
|
||||
strcat(log_str, " ");
|
||||
strcat(log_str, process_name);
|
||||
strcat(log_str, " ");
|
||||
strcat(log_str, str);
|
||||
// if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){
|
||||
// }
|
||||
// fclose(log_fp);
|
||||
|
||||
// }
|
||||
|
||||
{
|
||||
pthread_t pid;
|
||||
char *keepstr = strdup( log_str );
|
||||
pthread_create( &pid, NULL, record_log_thread, (void *)keepstr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void init_sem(int *semid, CONFIG_FILE_ST *ptr)
|
||||
{
|
||||
key_t key;
|
||||
char error_str[200];
|
||||
|
||||
/* write error in logfile */
|
||||
|
||||
/* write error in logfile */
|
||||
memset(error_str, 0, sizeof(error_str));
|
||||
#if 0
|
||||
if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) {
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno));
|
||||
record_log(error_str);
|
||||
return;
|
||||
}
|
||||
#else
|
||||
key = 0x1d5200;
|
||||
#endif
|
||||
|
||||
if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno));
|
||||
record_log(error_str);
|
||||
@@ -209,8 +282,12 @@ static void init_sem(int *semid, CONFIG_FILE_ST *ptr)
|
||||
}
|
||||
}
|
||||
|
||||
static int global_share_id = -1;
|
||||
static void *global_shmptr = NULL;
|
||||
|
||||
static void init_shm(SHM **ptr, CONFIG_FILE_ST *p)
|
||||
{
|
||||
#ifdef USE_OLD_MMSHARE
|
||||
int fd = -1;
|
||||
char error_str[200];
|
||||
|
||||
@@ -221,15 +298,66 @@ static void init_shm(SHM **ptr, CONFIG_FILE_ST *p)
|
||||
return;
|
||||
}
|
||||
umask(mask_tmp);
|
||||
lseek(fd, sizeof(SHM), SEEK_SET);
|
||||
write(fd, "\0", 1);
|
||||
*ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
lseek(fd, sizeof(SHM), SEEK_SET);
|
||||
write(fd, "\0", 1);
|
||||
*ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if(*ptr == MAP_FAILED){
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno));
|
||||
record_log(error_str);
|
||||
return;
|
||||
}
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
close(fd);
|
||||
#else
|
||||
void* shmptr = NULL;
|
||||
int size = 0;
|
||||
key_t share_key;
|
||||
// char *ref_count = NULL;
|
||||
char *flag_str = NULL;
|
||||
char *path = shm_path;
|
||||
char error_str[200];
|
||||
|
||||
size = sizeof(SHM);
|
||||
size = size + 16;
|
||||
|
||||
// share_key = ftok(path, 0x000d5000);
|
||||
share_key = 0x1d5010;
|
||||
|
||||
#if 0
|
||||
snprintf( error_str, sizeof( error_str ),
|
||||
"MSG: sys_nic, shm, size:%d, key:0x%x(%d)\n", size, share_key, share_key);
|
||||
record_log(error_str);
|
||||
#endif
|
||||
|
||||
global_share_id = shmget( share_key, size, IPC_CREAT|0666 );
|
||||
if( global_share_id == -1 ) {
|
||||
snprintf( error_str, sizeof( error_str ),
|
||||
"EMERG: fail shmget, %s, 0x%x\n", strerror(errno), share_key);
|
||||
record_log(error_str);
|
||||
return;
|
||||
}
|
||||
|
||||
shmptr = shmat( global_share_id, NULL, 0 );
|
||||
if( shmptr == (void *)-1 ) {
|
||||
snprintf( error_str, sizeof( error_str ),
|
||||
"EMERG: fail shmat, %s, %s\n", strerror(errno), path);
|
||||
record_log(error_str);
|
||||
return;
|
||||
}
|
||||
|
||||
flag_str = (char *)shmptr+size-16;
|
||||
|
||||
// if( strcmp( flag_str, "D5001" ) != 0 ) {
|
||||
//first, do init.
|
||||
memset( shmptr, 0, size );
|
||||
// strcpy( flag_str, "D5001" );
|
||||
// }
|
||||
|
||||
global_shmptr = shmptr;
|
||||
|
||||
// ref_count = (char *)shmptr+size-1;
|
||||
// (*ref_count)++;
|
||||
*ptr = (SHM *)shmptr;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -245,7 +373,7 @@ static void get_sem(int semid)
|
||||
lock[1].sem_num = 0;
|
||||
lock[1].sem_op = 1;
|
||||
lock[1].sem_flg = SEM_UNDO;
|
||||
|
||||
|
||||
while(semop(semid, lock, 2)){
|
||||
if(errno == EINTR)continue;
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno));
|
||||
@@ -448,13 +576,13 @@ static void *send_inc_info(void *p)
|
||||
return NULL;
|
||||
}
|
||||
s_time.tv_sec = ptr->conf->write_interval;
|
||||
s_time.tv_nsec = 0;
|
||||
|
||||
s_time.tv_nsec = 0;
|
||||
|
||||
/* copy all net_info*/
|
||||
while(1){
|
||||
i = 0;
|
||||
get_sem(ptr->semid);
|
||||
memset(ptr->shm_ptr, 0, sizeof(SHM));
|
||||
//memset(ptr->shm_ptr, 0, sizeof(SHM));
|
||||
curr = ptr->listp->next;
|
||||
while(curr != NULL){
|
||||
get_inc_info(&curr->info);
|
||||
@@ -489,7 +617,7 @@ static int get_netcard_count(char *name)
|
||||
|
||||
strncat(path, name, strlen(name));
|
||||
if((fp = fopen(path, "r")) == NULL)return 1;
|
||||
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
fseek(fp, 65, SEEK_SET);
|
||||
while((retp = fgets(buf, sizeof(buf), fp)) != NULL){
|
||||
@@ -521,10 +649,10 @@ static void *cacu_flow(void *p)
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n");
|
||||
record_log(error_str);
|
||||
pthread_mutex_unlock(&mut);
|
||||
return NULL;
|
||||
}
|
||||
s_time.tv_sec = ptr->mem.conf->flow_interval;
|
||||
s_time.tv_nsec = 0;
|
||||
return NULL;
|
||||
}
|
||||
s_time.tv_sec = ptr->mem.conf->flow_interval;
|
||||
s_time.tv_nsec = 0;
|
||||
|
||||
for(i = 0; i < MAXNICNUM ; i++){
|
||||
count_ok[i] = 0;
|
||||
@@ -533,9 +661,9 @@ static void *cacu_flow(void *p)
|
||||
while(1){
|
||||
i = 0;
|
||||
curr = ptr->mem.listp->next;
|
||||
while(curr != NULL){
|
||||
get_inc_info(&curr->info);
|
||||
get_inc_stats(&curr->info);
|
||||
while(curr != NULL){
|
||||
get_inc_info(&curr->info);
|
||||
get_inc_stats(&curr->info);
|
||||
if(flags){
|
||||
bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes;
|
||||
}
|
||||
@@ -622,8 +750,8 @@ static char *parse_str(char **s)
|
||||
|
||||
int ping_gw(char *devname)
|
||||
{
|
||||
FILE *fp;
|
||||
int i, ret = -1, j, k;
|
||||
FILE *fp;
|
||||
int i, j, k;
|
||||
char buf[128];
|
||||
char getbuf[64];
|
||||
|
||||
@@ -636,31 +764,29 @@ int ping_gw(char *devname)
|
||||
|
||||
for(k=0; k<conf.pingnum; ++k){
|
||||
for(j=0; j<conf.nic[i].gw_num; ++j){
|
||||
//snprintf(buf, sizeof(buf), "ping -c 1 -w %d %s > /dev/null 2>&1", conf.pinglap,conf.nic[i].ping_ip[j]);
|
||||
|
||||
snprintf(buf,sizeof(buf),"ping -c 1 -w %d %s|grep received|awk -F, '{print $2}'|awk '{print $1}'", conf.pinglap,conf.nic[i].ping_ip[j]);
|
||||
if((fp = popen(buf,"r")) == NULL)
|
||||
{
|
||||
perror("popen");
|
||||
return 0;
|
||||
}else{
|
||||
fgets(getbuf,sizeof(getbuf),fp);
|
||||
if(strncmp(getbuf,"1",1) == 0)
|
||||
{
|
||||
pclose(fp);
|
||||
return 0;
|
||||
}else{
|
||||
pclose(fp);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
pclose(fp);
|
||||
return 0;
|
||||
snprintf(buf,sizeof(buf),"ping -c 1 -w %d %s|grep received|awk -F, '{print $2}'|awk '{print $1}'", conf.pinglap,conf.nic[i].ping_ip[j]);
|
||||
if((fp = popen(buf,"r")) == NULL)
|
||||
{
|
||||
perror("popen");
|
||||
return 0;
|
||||
}else{
|
||||
fgets(getbuf,sizeof(getbuf),fp);
|
||||
if(strncmp(getbuf,"0",1) != 0)
|
||||
{
|
||||
pclose(fp);
|
||||
return 0;
|
||||
}else{
|
||||
pclose(fp);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
pclose(fp);
|
||||
return 0;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void *check_inc_switch(void *ptr)
|
||||
@@ -669,11 +795,11 @@ static void *check_inc_switch(void *ptr)
|
||||
FILE *fp = NULL;
|
||||
char name[NIC_NAME_LEN];
|
||||
struct timespec s_time, os_time;
|
||||
int count = 0;
|
||||
int count = 0;
|
||||
char linebuf[LINE_SIZE];
|
||||
char error_str[200];
|
||||
char *str = NULL;
|
||||
char *retp = NULL;
|
||||
char *str = NULL;
|
||||
char *retp = NULL;
|
||||
|
||||
if((fp = fopen(arg->bond_file_path, "r")) == NULL){
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno));
|
||||
@@ -691,9 +817,9 @@ static void *check_inc_switch(void *ptr)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
s_time.tv_sec = 0;
|
||||
s_time.tv_nsec = 100000000;
|
||||
s_time.tv_nsec = 100000000;
|
||||
|
||||
while(1){
|
||||
while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){
|
||||
@@ -739,7 +865,7 @@ static void *check_inc_switch(void *ptr)
|
||||
}
|
||||
free(ptr);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *create_logfile(void *ptr)
|
||||
@@ -801,7 +927,7 @@ void write_time(int semid, SHM *ptr)
|
||||
{
|
||||
int i = 0;
|
||||
int flag_time_stamp;
|
||||
|
||||
|
||||
flag_time_stamp = time(NULL);
|
||||
|
||||
while(ptr->info[i].charname[0] != 0){
|
||||
@@ -827,7 +953,7 @@ char *parse(char *line)
|
||||
void release_name_list(NET_NAME_ST *head)
|
||||
{
|
||||
NET_NAME_ST *curr, *save;
|
||||
|
||||
|
||||
curr = head->next;
|
||||
while(curr != NULL){
|
||||
save = curr->next;
|
||||
@@ -838,7 +964,7 @@ void release_name_list(NET_NAME_ST *head)
|
||||
int get_netcard(char *line, char *nic)
|
||||
{
|
||||
char *begin, *retp;
|
||||
|
||||
|
||||
while(isspace(*line))line++;
|
||||
begin = line;
|
||||
while(!isspace(*line))line++;
|
||||
@@ -854,7 +980,7 @@ int get_netcard(char *line, char *nic)
|
||||
|
||||
void sig_handler(int sig)
|
||||
{
|
||||
// char err_str[200];
|
||||
// char err_str[200];
|
||||
|
||||
#if 0
|
||||
if(unlink(SHM_PATH) == -1){
|
||||
@@ -866,10 +992,10 @@ void sig_handler(int sig)
|
||||
record_log(err_str);
|
||||
}
|
||||
#endif
|
||||
// semctl(sem_id, 0, IPC_RMID);
|
||||
// semctl(sem_id, 0, IPC_RMID);
|
||||
|
||||
// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig);
|
||||
// record_log(err_str);
|
||||
// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig);
|
||||
// record_log(err_str);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -885,7 +1011,7 @@ int find_nic_config(char *dev_name){
|
||||
|
||||
for(i=0; i<nicnum; ++i)
|
||||
if(!strcmp(dev_name, conf.nic[i].dev_name))
|
||||
index = i;
|
||||
index = i;
|
||||
|
||||
return index;
|
||||
}
|
||||
@@ -897,7 +1023,7 @@ DOTCONF_CB(cb_str)
|
||||
char wrong_ip[125] = {0};
|
||||
|
||||
//"ping"选项的处理
|
||||
if((strcmp(cmd ->name, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){
|
||||
if((strcmp(cmd ->name, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){
|
||||
i = 1;
|
||||
if(cmd->data.list[0] && cmd->data.list[1]){
|
||||
//查找设备列表中是否已有该网卡
|
||||
@@ -907,43 +1033,43 @@ DOTCONF_CB(cb_str)
|
||||
//如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的
|
||||
index = find_nic_config(cmd->data.list[0]);
|
||||
if(index == -1){
|
||||
strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0]));
|
||||
strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0]));
|
||||
card_info_index = nicnum;
|
||||
nicnum++;
|
||||
nicnum++;
|
||||
}
|
||||
else
|
||||
card_info_index = index;
|
||||
|
||||
|
||||
while(cmd->data.list[i]){
|
||||
if((int)(inet_addr(cmd->data.list[i])) == -1){
|
||||
sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]);
|
||||
strcat(wrong_ip,"\n");
|
||||
record_log(wrong_ip);
|
||||
} else{
|
||||
strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i]));
|
||||
strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i]));
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//"nic"选项的处理
|
||||
if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){
|
||||
if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){
|
||||
//设备列表中没有给定的网卡设备,则添加一项
|
||||
//否则不必添加
|
||||
//对应配置文件中先指定"ping"后指定"nic"的情况
|
||||
index = find_nic_config(cmd->data.str);
|
||||
if(index == -1){
|
||||
strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str));
|
||||
nicnum++;
|
||||
strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str));
|
||||
nicnum++;
|
||||
}
|
||||
}
|
||||
|
||||
if((strcmp(cmd ->name, myconf[11]) == 0)){
|
||||
strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str));
|
||||
}
|
||||
|
||||
return NULL;
|
||||
if((strcmp(cmd ->name, myconf[11]) == 0)){
|
||||
strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str));
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DOTCONF_CB(cb_int)
|
||||
@@ -981,43 +1107,43 @@ DOTCONF_CB(cb_int)
|
||||
if(strcmp(cmd ->name, myconf[15]) == 0){
|
||||
conf.pinglap = cmd->data.value;
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DOTCONF_CB(cb_list)
|
||||
{
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int get_conf()
|
||||
{
|
||||
// int uid;
|
||||
// char buf[1024];
|
||||
// int i;
|
||||
// struct passwd *user;
|
||||
configfile_t *configfile;
|
||||
|
||||
// memset(buf, 0, sizeof(buf));
|
||||
// uid=getuid();
|
||||
// user = getpwnam("d5000");
|
||||
// user = getpwuid(uid);
|
||||
|
||||
// sprintf(buf,"%s", user->pw_dir);
|
||||
// strcat(buf,conf_path);
|
||||
// int uid;
|
||||
// char buf[1024];
|
||||
// int i;
|
||||
// struct passwd *user;
|
||||
configfile_t *configfile;
|
||||
|
||||
configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE);
|
||||
if (!configfile)
|
||||
{
|
||||
record_log("Warning can't reading config file\n");
|
||||
return -1;
|
||||
}
|
||||
// memset(buf, 0, sizeof(buf));
|
||||
// uid=getuid();
|
||||
// user = getpwnam("d5000");
|
||||
// user = getpwuid(uid);
|
||||
|
||||
if (dotconf_command_loop(configfile) == 0){
|
||||
// sprintf(buf,"%s", user->pw_dir);
|
||||
// strcat(buf,conf_path);
|
||||
|
||||
configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE);
|
||||
if (!configfile)
|
||||
{
|
||||
record_log("Warning can't reading config file\n");
|
||||
return -1;
|
||||
}
|
||||
dotconf_cleanup(configfile);
|
||||
|
||||
if (dotconf_command_loop(configfile) == 0){
|
||||
record_log("Warning can't reading config file\n");
|
||||
return -1;
|
||||
}
|
||||
dotconf_cleanup(configfile);
|
||||
|
||||
if(conf.domain == 0)
|
||||
conf.domain = 10;
|
||||
@@ -1051,21 +1177,21 @@ int get_conf()
|
||||
|
||||
int isrun(char *p)
|
||||
{
|
||||
char buf[16];
|
||||
char cmdbuf[1024];
|
||||
FILE *fp;
|
||||
char buf[16];
|
||||
char cmdbuf[1024];
|
||||
FILE *fp;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
memset(cmdbuf, 0, sizeof(cmdbuf));
|
||||
memset(buf, 0, sizeof(buf));
|
||||
memset(cmdbuf, 0, sizeof(cmdbuf));
|
||||
|
||||
snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p);
|
||||
fp=popen(cmdbuf, "r");
|
||||
fread(buf, sizeof(char), sizeof(buf), fp);
|
||||
snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p);
|
||||
fp=popen(cmdbuf, "r");
|
||||
fread(buf, sizeof(char), sizeof(buf), fp);
|
||||
pclose(fp);
|
||||
if(strchr(buf, ' ') != NULL)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
if(strchr(buf, ' ') != NULL)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_broad_ip(void)
|
||||
@@ -1074,17 +1200,17 @@ int get_broad_ip(void)
|
||||
char error_str[200];
|
||||
int skfd;
|
||||
struct ifreq ifr;
|
||||
|
||||
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
memset(ip_p, 0, sizeof(ip_p));
|
||||
|
||||
|
||||
if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1;
|
||||
strcpy(ifr.ifr_name, conf.udp);
|
||||
if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){
|
||||
sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]);
|
||||
sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]);
|
||||
sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]);
|
||||
sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]);
|
||||
sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]);
|
||||
sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]);
|
||||
sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]);
|
||||
sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]);
|
||||
}else{
|
||||
snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp);
|
||||
record_log(error_str);
|
||||
@@ -1128,6 +1254,7 @@ void create_dir(char *create_path)
|
||||
}
|
||||
void create_semdir()
|
||||
{
|
||||
#if 0
|
||||
int ret = 0;
|
||||
mode_t mask_tmp;
|
||||
|
||||
@@ -1137,9 +1264,11 @@ void create_semdir()
|
||||
close(ret);
|
||||
}
|
||||
umask(mask_tmp);
|
||||
#endif
|
||||
}
|
||||
void create_shmdir()
|
||||
{
|
||||
#if 0
|
||||
int ret = 0;
|
||||
mode_t mask_tmp;
|
||||
|
||||
@@ -1149,6 +1278,7 @@ void create_shmdir()
|
||||
close(ret);
|
||||
}
|
||||
umask(mask_tmp);
|
||||
#endif
|
||||
}
|
||||
void create_confdir()
|
||||
{
|
||||
@@ -1178,13 +1308,13 @@ void create_logdir()
|
||||
|
||||
umask(mask_tmp);
|
||||
}
|
||||
/* create netcard name list */
|
||||
/* create netcard name list */
|
||||
int create_name_list(NET_NAME_ST *all_name_list, NET_NAME_ST *opt_name_list)
|
||||
{
|
||||
int i, count_t, err_val = 0;
|
||||
NET_NAME_ST *curr, *node;
|
||||
char error_str[200];
|
||||
|
||||
|
||||
for(i = 0, count_t = 0; conf.nic[i].dev_name[0] != 0 && (i < MAXNICNUM); i++){
|
||||
curr = all_name_list->next;
|
||||
while(curr != NULL){
|
||||
@@ -1232,7 +1362,7 @@ int get_netcard_status(NETCARD_INFO_ST *listp)
|
||||
if(listp->status == NIC_UNLINKABLE)
|
||||
return NIC_UNLINKABLE;
|
||||
}
|
||||
|
||||
|
||||
return NIC_NORMAL;
|
||||
}
|
||||
|
||||
@@ -1309,8 +1439,8 @@ int main(int argc, char ** argv)
|
||||
D5000_NIC_ALARM Malarm;
|
||||
struct sigaction sig;
|
||||
struct passwd *user;
|
||||
// proc_invocation prcm;
|
||||
|
||||
proc_invocation prcm;
|
||||
|
||||
char log_record[125]={0};
|
||||
|
||||
memset(&conf, 0, sizeof(CONFIG_FILE_ST));
|
||||
@@ -1318,8 +1448,8 @@ int main(int argc, char ** argv)
|
||||
printf("the program is already started!\n");
|
||||
exit(0);
|
||||
}
|
||||
//process register
|
||||
#if 0
|
||||
//process register
|
||||
#if 1
|
||||
if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){
|
||||
perror("proc_init()");
|
||||
exit(-1);
|
||||
@@ -1347,7 +1477,7 @@ int main(int argc, char ** argv)
|
||||
strcat(shm_path,SHM_PATH);
|
||||
strcat(sem_path,SEM_PATH);
|
||||
strcat(conf_path,CONF_FILE);
|
||||
|
||||
|
||||
create_semdir();
|
||||
create_shmdir();
|
||||
create_logdir();
|
||||
@@ -1549,6 +1679,43 @@ int main(int argc, char ** argv)
|
||||
pthread_join(tid_time, NULL);
|
||||
pthread_mutex_destroy(&mut);
|
||||
globfree(&res);
|
||||
#ifdef USE_OLD_MMSHARE
|
||||
munmap(shm_ptr, sizeof(SHM));
|
||||
#else
|
||||
{
|
||||
|
||||
void *shmptr = shm_ptr;
|
||||
#if 0
|
||||
snprintf(error_str, sizeof(error_str), "NOTICE: free shm %p, glo:%p\n", shmptr, global_shmptr);
|
||||
record_log(error_str);
|
||||
#endif
|
||||
|
||||
if( shmptr == global_shmptr ) {
|
||||
char *ref_count = NULL;
|
||||
int is_last = 0;
|
||||
int size = sizeof(SHM);
|
||||
|
||||
ref_count = (char *)shmptr + size + 16 - 1;
|
||||
|
||||
is_last = *ref_count == 1?1:0;
|
||||
#if 0
|
||||
snprintf(error_str, sizeof(error_str), "NOTICE: sys_nic, size:%d, ref_count: %d, last:%d\n",
|
||||
size, *ref_count, is_last);
|
||||
record_log(error_str);
|
||||
#endif
|
||||
|
||||
// if( is_last ) {
|
||||
memset( shmptr, 0, size + 16 );
|
||||
// }
|
||||
|
||||
shmdt( shmptr );
|
||||
// shmctl( global_share_id, IPC_RMID, NULL );
|
||||
// if( is_last ) {
|
||||
// global_shmptr = NULL;
|
||||
// global_share_id = -1;
|
||||
// }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/sem.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
@@ -12,11 +13,12 @@
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
#include <pwd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "nicinfo_shm.h"
|
||||
|
||||
#define SHM_PATH "/tmp/sys_netcard_shm_path"
|
||||
#define SEM_PATH "/tmp/sys_netcard_sem_path"
|
||||
#define SHM_PATH "/share/sys_netcard_shm_path"
|
||||
#define SEM_PATH "/share/sys_netcard_sem_path"
|
||||
#define LOG_PATH "/var/log/netcard/"
|
||||
|
||||
typedef struct net_info{
|
||||
@@ -29,9 +31,64 @@ static char shm_path[1024];
|
||||
static char sem_path[1024];
|
||||
SHM *ptr = NULL;
|
||||
|
||||
|
||||
static void create_dir(char *create_path)
|
||||
{
|
||||
int i,len = strlen(create_path);
|
||||
|
||||
len = strlen(create_path);
|
||||
|
||||
for(i=1; i<len; i++)
|
||||
{
|
||||
if(create_path[i]=='/')
|
||||
{
|
||||
create_path[i] = 0;
|
||||
if( access(create_path, F_OK)!=0 )
|
||||
{
|
||||
if(mkdir(create_path, 0755)==-1)
|
||||
{
|
||||
perror("mkdir error");
|
||||
}
|
||||
}
|
||||
create_path[i] = '/';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static pthread_mutex_t lock_record_log = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define LOG_LOCK pthread_mutex_lock(&lock_record_log)
|
||||
#define LOG_UNLOCK pthread_mutex_unlock(&lock_record_log)
|
||||
|
||||
static void linx_free_shm( void *addr, int size );
|
||||
static int is_checkdir = 0;
|
||||
|
||||
static void *record_log_thread( void *arg )
|
||||
{
|
||||
char *log_str = (char *)arg;
|
||||
FILE *log_fp = NULL;
|
||||
|
||||
LOG_LOCK;
|
||||
if( !is_checkdir ) {
|
||||
create_dir( log_path );
|
||||
is_checkdir = 1;
|
||||
}
|
||||
create_dir( log_path );
|
||||
if((log_fp = fopen(log_path, "a")) != NULL){
|
||||
if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){
|
||||
}
|
||||
fclose(log_fp);
|
||||
}
|
||||
|
||||
LOG_UNLOCK;
|
||||
free(log_str);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* write error in logfile */
|
||||
void record_log(char *str)
|
||||
{
|
||||
#if 0
|
||||
time_t tamp;
|
||||
char str_tm[4];
|
||||
char log_str[512];
|
||||
@@ -59,6 +116,41 @@ void record_log(char *str)
|
||||
}
|
||||
fclose(log_fp);
|
||||
}
|
||||
#else
|
||||
time_t tamp;
|
||||
char str_tm[4];
|
||||
char log_str[512];
|
||||
// FILE *log_fp = NULL;
|
||||
struct tm tmptr;
|
||||
struct timeval tv;
|
||||
struct timezone tz;
|
||||
|
||||
// if((log_fp = fopen(log_path, "a")) != NULL){
|
||||
tamp = time(NULL);
|
||||
memset(str_tm, 0, sizeof(str_tm));
|
||||
memset(log_str, 0, sizeof(log_str));
|
||||
localtime_r(&tamp, &tmptr);
|
||||
gettimeofday(&tv, &tz);
|
||||
snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000);
|
||||
if(str_tm[1] == '\0')str_tm[1] = '0';
|
||||
if(str_tm[2] == '\0')str_tm[2] = '0';
|
||||
strftime(log_str, sizeof(log_str), "%F %T.", &tmptr);
|
||||
strcat(log_str, str_tm);
|
||||
strcat(log_str, " ");
|
||||
strcat(log_str, "get_nic_info");
|
||||
strcat(log_str, " ");
|
||||
strcat(log_str, str);
|
||||
// if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){
|
||||
// }
|
||||
// fclose(log_fp);
|
||||
// }
|
||||
|
||||
{
|
||||
pthread_t pid;
|
||||
char *keepstr = strdup( log_str );
|
||||
pthread_create( &pid, NULL, record_log_thread, (void *)keepstr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void get_sem(int semid)
|
||||
@@ -101,6 +193,8 @@ void release_sem(int semid)
|
||||
}
|
||||
}
|
||||
|
||||
static int global_share_id = -1;
|
||||
static void *global_shmptr = NULL;
|
||||
|
||||
int init_nic_info(void)
|
||||
{
|
||||
@@ -109,12 +203,15 @@ int init_nic_info(void)
|
||||
char err_str[200];
|
||||
key_t key;
|
||||
|
||||
|
||||
#if 0
|
||||
if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) {
|
||||
snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno));
|
||||
record_log(err_str);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
key = 0x1d5200;
|
||||
#endif
|
||||
if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){
|
||||
snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno));
|
||||
record_log(err_str);
|
||||
@@ -136,6 +233,7 @@ int init_nic_info(void)
|
||||
}
|
||||
}
|
||||
*/
|
||||
#ifdef USE_OLD_MMSHARE
|
||||
if((fd = open(shm_path, O_RDONLY)) == -1){
|
||||
snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno));
|
||||
record_log(err_str);
|
||||
@@ -148,9 +246,63 @@ int init_nic_info(void)
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
#else
|
||||
{
|
||||
void* shmptr = NULL;
|
||||
int size = 0;
|
||||
key_t share_key;
|
||||
int share_id = 0;
|
||||
char *ref_count = NULL;
|
||||
char *flag_str = NULL;
|
||||
char *path = shm_path;
|
||||
|
||||
size = sizeof(SHM);
|
||||
size = size + 16;
|
||||
|
||||
// share_key = ftok(path, 0x000d5000);
|
||||
share_key = 0x1d5010;
|
||||
|
||||
#if 0
|
||||
snprintf( err_str, sizeof( err_str ),
|
||||
"MSG: lib, shm, size:%d, key:0x%x(%d)\n", size, share_key, share_key);
|
||||
record_log(err_str);
|
||||
#endif
|
||||
|
||||
global_share_id = shmget( share_key, size, IPC_CREAT|0666 );
|
||||
if( global_share_id == -1 ) {
|
||||
snprintf( err_str, sizeof( err_str ),
|
||||
"EMERG: fail shmget, %s, 0x%x\n", strerror(errno), share_key);
|
||||
record_log(err_str);
|
||||
return -1;
|
||||
}
|
||||
|
||||
shmptr = shmat( global_share_id, NULL, 0 );
|
||||
if( shmptr == (void *)-1 ) {
|
||||
snprintf( err_str, sizeof( err_str ), "EMERG: shmat fail.%s,0x%x\n", strerror(errno), share_key );
|
||||
record_log( err_str );
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
flag_str = (char *)shmptr+size-16;
|
||||
if( strcmp( flag_str, "D5001" ) != 0 ) {
|
||||
//first, do init.
|
||||
memset( shmptr, 0, size );
|
||||
strcpy( flag_str, "D5001" );
|
||||
}
|
||||
|
||||
ref_count = (char *)shmptr+size-1;
|
||||
(*ref_count)++;
|
||||
#endif
|
||||
|
||||
global_shmptr = shmptr;
|
||||
ptr = (SHM *)shmptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
|
||||
{
|
||||
int i,ret;
|
||||
@@ -161,7 +313,10 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
|
||||
char buf[128];
|
||||
char err_str[200];
|
||||
struct passwd *user;
|
||||
|
||||
#if 0
|
||||
snprintf(err_str, sizeof(err_str), "NOTICE: get nicinfo:%s !\n", nic_name);
|
||||
record_log(err_str);
|
||||
#endif
|
||||
tamp = time(NULL);
|
||||
localtime_r(&tamp, &tmptr);
|
||||
|
||||
@@ -173,13 +328,16 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
|
||||
strcat(log_path,LOG_PATH);
|
||||
strcat(shm_path,SHM_PATH);
|
||||
strcat(sem_path,SEM_PATH);
|
||||
|
||||
#if 0
|
||||
//remove
|
||||
if((dir = opendir(log_path)) == NULL){
|
||||
if(errno == ENOENT){
|
||||
strcpy(log_path, "/tmp/");
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
#endif
|
||||
|
||||
s = strrchr(log_path, '/');
|
||||
s++;
|
||||
*s = '\0';
|
||||
@@ -197,13 +355,63 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
|
||||
// get_sem(semid);
|
||||
memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO));
|
||||
release_sem(semid);
|
||||
#ifdef USE_OLD_MMSHARE
|
||||
munmap(ptr, sizeof(SHM));
|
||||
#else
|
||||
linx_free_shm( ptr, sizeof(SHM) );
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
release_sem(semid);
|
||||
snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name);
|
||||
record_log(err_str);
|
||||
#ifdef USE_OLD_MMSHARE
|
||||
munmap(ptr, sizeof(SHM));
|
||||
#else
|
||||
linx_free_shm( ptr, sizeof(SHM) );
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void linx_free_shm( void *addr, int size )
|
||||
{
|
||||
|
||||
void *shmptr = addr;
|
||||
char err_str[200];
|
||||
|
||||
#if 0
|
||||
snprintf(err_str, sizeof(err_str), "NOTICE: free shm 0x%x, size:%d, glo:0x%x\n", addr, size, global_shmptr);
|
||||
record_log(err_str);
|
||||
#endif
|
||||
|
||||
if( shmptr == global_shmptr ) {
|
||||
char *ref_count = NULL;
|
||||
int is_last = 0;
|
||||
// int size = sizeof(SHM);
|
||||
|
||||
ref_count = (char *)shmptr + size + 16 - 1;
|
||||
|
||||
is_last = *ref_count == 1?1:0;
|
||||
|
||||
#if 0
|
||||
snprintf(err_str, sizeof(err_str), "NOTICE: lib, sref_count: %d, last:%d\n", *ref_count, is_last);
|
||||
record_log(err_str);
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
if( is_last ) {
|
||||
memset( shmptr, 0, size + 16 );
|
||||
}
|
||||
#endif
|
||||
shmdt( shmptr );
|
||||
// shmctl( global_share_id, IPC_RMID, NULL );
|
||||
// if( is_last ) {
|
||||
// global_shmptr = NULL;
|
||||
// global_share_id = -1;
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,6 @@ all:read_shm recv_msg getmem
|
||||
|
||||
getmem:getmem.c
|
||||
# g++ -g -L ../dynamic_lib -lnic_shm -o $@ $^
|
||||
g++ -g -lnic_shm -o $@ $^
|
||||
g++ -g -L ${HOME}/lib -lpthread -lnic_shm -o $@ $^
|
||||
clean:
|
||||
rm getmem read_shm recv_msg
|
||||
|
||||
Reference in New Issue
Block a user