整理代码库,删除无用分支以及代码

重命名:   trunk/src/README -> ../README
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/Makefile
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/Makefile.config
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/const.h
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/dotconf.h
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/mnic.c
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/mnic.h
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.c
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.h
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/proc_inv.h
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/read_netcard.c
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/send_alarm.c
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/sys_netcard.h
	删除:     ../branches/sys_nicmonitor-1.4-龙芯-32/sys_nicmonitor
	删除:     ../branches/sys_nicmonitor-1.5/Makefile
	删除:     ../branches/sys_nicmonitor-1.5/Makefile.config
	删除:     ../branches/sys_nicmonitor-1.5/README1.5
	删除:     ../branches/sys_nicmonitor-1.5/const.h
	删除:     ../branches/sys_nicmonitor-1.5/mnic.c
	删除:     ../branches/sys_nicmonitor-1.5/mnic.h
	删除:     ../branches/sys_nicmonitor-1.5/nicinfo_shm.c
	删除:     ../branches/sys_nicmonitor-1.5/nicinfo_shm.h
	删除:     ../branches/sys_nicmonitor-1.5/proc_inv.h
	删除:     ../branches/sys_nicmonitor-1.5/read_netcard.c
	删除:     ../branches/sys_nicmonitor-1.5/send_alarm.c
	删除:     ../branches/sys_nicmonitor-1.5/sys_netcard.h
	删除:     ../branches/sys_nicmonitor-1.5/sys_nicmonitor
	删除:     ../branches/sys_nicmonitor-1.6/Makefile
	删除:     ../branches/sys_nicmonitor-1.6/Makefile.config
	删除:     ../branches/sys_nicmonitor-1.6/README1.5
	删除:     ../branches/sys_nicmonitor-1.6/README1.6
	删除:     ../branches/sys_nicmonitor-1.6/const.h
	删除:     ../branches/sys_nicmonitor-1.6/mnic.c
	删除:     ../branches/sys_nicmonitor-1.6/mnic.h
	删除:     ../branches/sys_nicmonitor-1.6/nicinfo_shm.c
	删除:     ../branches/sys_nicmonitor-1.6/nicinfo_shm.h
	删除:     ../branches/sys_nicmonitor-1.6/proc_inv.h
	删除:     ../branches/sys_nicmonitor-1.6/read_netcard.c
	删除:     ../branches/sys_nicmonitor-1.6/send_alarm.c
	删除:     ../branches/sys_nicmonitor-1.6/sys_netcard.h
	删除:     ../branches/sys_nicmonitor-1.6/sys_nicmonitor
	重命名:   trunk/sys_nicmonitor/src/Pkgfile -> Pkgfile
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/README -> README
	删除:     branches/sys_nicmonitor-1.0/Makefile
	删除:     branches/sys_nicmonitor-1.0/Makefile.config
	删除:     branches/sys_nicmonitor-1.0/const.h
	删除:     branches/sys_nicmonitor-1.0/mnic.c
	删除:     branches/sys_nicmonitor-1.0/mnic.h
	删除:     branches/sys_nicmonitor-1.0/nicinfo_shm.c
	删除:     branches/sys_nicmonitor-1.0/nicinfo_shm.h
	删除:     branches/sys_nicmonitor-1.0/proc_inv.h
	删除:     branches/sys_nicmonitor-1.0/read_netcard.c
	删除:     branches/sys_nicmonitor-1.0/send_alarm.c
	删除:     branches/sys_nicmonitor-1.0/sys_netcard.h
	重命名:   trunk/sys_nicmonitor/src/build.sh -> build.sh
	重命名:   trunk/sys_nicmonitor/src/dep/dotconf#1.0.13-1.pkg.tar.gz -> dep/dotconf#1.0.13-1.pkg.tar.gz
	重命名:   trunk/sys_nicmonitor/src/dep/libman.so -> dep/libman.so
	重命名:   trunk/sys_nicmonitor/src/dep/run.sh -> dep/run.sh
	重命名:   trunk/sys_nicmonitor/src/post_add.sh -> post_add.sh
	重命名:   trunk/sys_nicmonitor/src/post_mk.sh -> post_mk.sh
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile -> sys_nicmonitor/Makefile
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile.config -> sys_nicmonitor/Makefile.config
	重命名:   trunk/sys_nicmonitor/src/README -> sys_nicmonitor/README
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/common.c -> sys_nicmonitor/common.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/const.h -> sys_nicmonitor/const.h
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c -> sys_nicmonitor/mnic.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.h -> sys_nicmonitor/mnic.h
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.c -> sys_nicmonitor/nicinfo_shm.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.h -> sys_nicmonitor/nicinfo_shm.h
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/proc_inv.h -> sys_nicmonitor/proc_inv.h
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/read_netcard.c -> sys_nicmonitor/read_netcard.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/send_alarm.c -> sys_nicmonitor/send_alarm.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/sys_netcard.h -> sys_nicmonitor/sys_netcard.h
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/test.c -> sys_nicmonitor/test.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/testread.c -> sys_nicmonitor/testread.c
	重命名:   trunk/sys_nicmonitor/src/sys_nicmonitor/version.h -> sys_nicmonitor/version.h
	删除:     tags/README1.5
	删除:     tags/README1.6
	删除:     tags/README1.7
	删除:     tags/libman.so
	删除:     tags/sys_nicmonitor-1.0.0/Makefile
	删除:     tags/sys_nicmonitor-1.0.0/Makefile.config
	删除:     tags/sys_nicmonitor-1.0.0/const.h
	删除:     tags/sys_nicmonitor-1.0.0/mnic.c
	删除:     tags/sys_nicmonitor-1.0.0/mnic.h
	删除:     tags/sys_nicmonitor-1.0.0/nicinfo_shm.c
	删除:     tags/sys_nicmonitor-1.0.0/nicinfo_shm.h
	删除:     tags/sys_nicmonitor-1.0.0/proc_inv.h
	删除:     tags/sys_nicmonitor-1.0.0/read_netcard.c
	删除:     tags/sys_nicmonitor-1.0.0/send_alarm.c
	删除:     tags/sys_nicmonitor-1.0.0/sys_netcard.h
	删除:     tags/sys_nicmonitor-1.5/Makefile
	删除:     tags/sys_nicmonitor-1.5/Makefile.config
	删除:     tags/sys_nicmonitor-1.5/const.h
	删除:     tags/sys_nicmonitor-1.5/mnic.c
	删除:     tags/sys_nicmonitor-1.5/mnic.h
	删除:     tags/sys_nicmonitor-1.5/nicinfo_shm.c
	删除:     tags/sys_nicmonitor-1.5/nicinfo_shm.h
	删除:     tags/sys_nicmonitor-1.5/proc_inv.h
	删除:     tags/sys_nicmonitor-1.5/read_netcard.c
	删除:     tags/sys_nicmonitor-1.5/send_alarm.c
	删除:     tags/sys_nicmonitor-1.5/sys_netcard.h
	删除:     tags/sys_nicmonitor-1.6/Makefile
	删除:     tags/sys_nicmonitor-1.6/Makefile.config
	删除:     tags/sys_nicmonitor-1.6/const.h
	删除:     tags/sys_nicmonitor-1.6/mnic.c
	删除:     tags/sys_nicmonitor-1.6/mnic.h
	删除:     tags/sys_nicmonitor-1.6/nicinfo_shm.c
	删除:     tags/sys_nicmonitor-1.6/nicinfo_shm.h
	删除:     tags/sys_nicmonitor-1.6/proc_inv.h
	删除:     tags/sys_nicmonitor-1.6/read_netcard.c
	删除:     tags/sys_nicmonitor-1.6/send_alarm.c
	删除:     tags/sys_nicmonitor-1.6/sys_netcard.h
	删除:     tags/sys_nicmonitor-1.7/Makefile
	删除:     tags/sys_nicmonitor-1.7/Makefile.config
	删除:     tags/sys_nicmonitor-1.7/const.h
	删除:     tags/sys_nicmonitor-1.7/mnic.c
	删除:     tags/sys_nicmonitor-1.7/mnic.h
	删除:     tags/sys_nicmonitor-1.7/nicinfo_shm.c
	删除:     tags/sys_nicmonitor-1.7/nicinfo_shm.h
	删除:     tags/sys_nicmonitor-1.7/proc_inv.h
	删除:     tags/sys_nicmonitor-1.7/read_netcard.c
	删除:     tags/sys_nicmonitor-1.7/send_alarm.c
	删除:     tags/sys_nicmonitor-1.7/sys_netcard.h
	删除:     tags/sys_nicmonitor-1.8/bin/README
	删除:     tags/sys_nicmonitor-1.8/bin/install.sh
	删除:     tags/sys_nicmonitor-1.8/bin/sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
	删除:     tags/sys_nicmonitor-1.8/doc/网卡监视程序动态库使用手册【新】.doc
	删除:     tags/sys_nicmonitor-1.8/doc/网卡监视程序安装说明以及使用手册【新】.doc
	删除:     tags/sys_nicmonitor-1.8/src/Pkgfile
	删除:     tags/sys_nicmonitor-1.8/src/README1.8
	删除:     tags/sys_nicmonitor-1.8/src/V1.8
	删除:     tags/sys_nicmonitor-1.8/src/build.sh
	删除:     tags/sys_nicmonitor-1.8/src/post_add.sh
	删除:     tags/sys_nicmonitor-1.8/src/post_mk.sh
	删除:     tags/sys_nicmonitor-1.8/src/sys_nicmonitor-1.8.tar.gz
	删除:     tags/sys_nicmonitor-1.8/test/Makefile
	删除:     tags/sys_nicmonitor-1.8/test/dotconf.h
	删除:     tags/sys_nicmonitor-1.8/test/getmem
	删除:     tags/sys_nicmonitor-1.8/test/getmem.c
	删除:     tags/sys_nicmonitor-1.8/test/mnic.h
	删除:     tags/sys_nicmonitor-1.8/test/nicinfo_shm.h
	删除:     tags/sys_nicmonitor-1.8/test/read_shm
	删除:     tags/sys_nicmonitor-1.8/test/read_shm.c
	删除:     tags/sys_nicmonitor-1.8/test/recv_msg
	删除:     tags/sys_nicmonitor-1.8/test/recv_msg.c
	删除:     tags/sys_nicmonitor-1.8/test/sys_netcard.h
	重命名:   trunk/test/Makefile -> test/Makefile
	重命名:   trunk/test/dotconf.h -> test/dotconf.h
	重命名:   trunk/test/getmem.c -> test/getmem.c
	重命名:   trunk/test/mnic.h -> test/mnic.h
	重命名:   trunk/test/nicinfo_shm.h -> test/nicinfo_shm.h
	重命名:   trunk/test/read_shm.c -> test/read_shm.c
	重命名:   trunk/test/recv_msg.c -> test/recv_msg.c
	重命名:   trunk/test/sys_netcard.h -> test/sys_netcard.h
	删除:     trunk/LATEST
	删除:     trunk/README
	删除:     trunk/bin/README
	删除:     trunk/bin/install.sh
	删除:     trunk/bin/sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz
	删除:     trunk/doc/网卡监视程序动态库使用手册【新】.doc
	删除:     trunk/doc/网卡监视程序安装说明以及使用手册【新】.doc
	删除:     trunk/src/Pkgfile
	删除:     trunk/src/build.sh
	删除:     trunk/src/post_add.sh
	删除:     trunk/src/post_mk.sh
	删除:     trunk/src/sys_nicmonitor-1.8.tar.gz
	删除:     trunk/src/sys_nicmonitor-1.9.tar.gz
	删除:     trunk/sys_nicmonitor/README.md
	删除:     trunk/sys_nicmonitor/sys_nicmonitor-from-svn.tar.bz2

Signed-off-by: Zhang, Guodong <gdzhang@linx-info.com>
This commit is contained in:
Zhang, Guodong
2016-09-19 14:24:03 +08:00
parent c8d3ac9c6f
commit e0b7a7e83d
170 changed files with 0 additions and 20331 deletions

View File

@@ -0,0 +1,32 @@
#! /usr/bin/make -f
DEFAULTS = Makefile.config
include $(DEFAULTS)
all:sys_nicmonitor libnic_shm.so test
CFLAGS += -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral #-g
sys_nicmonitor:mnic.o read_netcard.o send_alarm.o
$(CC) -lman -ldotconf -lpthread -o $@ $^
libnic_shm.so:nicinfo_shm.c
$(CC) -fpic -shared -o $@ $^
test:test.c
gcc -o test test.c -lnic_shm -L=./ -lpthread
runtest:
LD_LIBRARY_PATH=./ ./test bond0 5000 &
clean:
$(RM) *.o
$(RM) libnic_shm.so sys_nicmonitor test
install:
$(MKDIR) -p $(LIBDIR)
$(MKDIR) -p $(BINDIR)
$(MKDIR) -p $(INCDIR)
$(CP) libnic_shm.so $(LIBDIR)
$(CP) sys_nicmonitor $(BINDIR)
$(CP) nicinfo_shm.h $(INCDIR)
uninstall:
$(RM) -f $(LIBDIR)/libnic_shm.so
$(RM) -f $(BINDIR)/sys_nicmonitor
$(RM) -f $(INCDIR)/nicinfo_shm.h

View File

@@ -0,0 +1,16 @@
PREFIX = $(DESTDIR)
BINDIR = $(PREFIX)/usr/bin
LIBDIR = $(PREFIX)/usr/lib64
INCDIR = $(PREFIX)/usr/include
INSTALL_SH = ./install.sh
CP = cp
RM = rm
#CC = gcc
CC = g++
MKDIR = mkdir

137
code/sys_nicmonitor/README Normal file
View File

@@ -0,0 +1,137 @@
--V1.14 --
20151229
修正了common.c中对日志消息队列的操作其中包括交换log_prepare与log_writing>队列指针的bug以及在写入log_prepare队列时未检查已写入消息是否大于队列长度的b
ug。修正了log_path(即日志文件名不会随时间改变的bug。
--V1.13 --
20151223
修正在记录日志中检查目录时在函数create_dir中由于采用线程进行日志记录多次调用get_nic_info可能会导致的将日志文件路径名变错的问题。在get_nic_info中判断log_path是否被初始化过如过未初始化则到共享内存中读取。否则为初始化过直接使用该值,并在create_dir中使用局部变量保存该值在create_dir中使用操作该局部变量而不去改变该全局变量的值。
-V1.12 --
20151223
修正在程序开始时对IPC变量删除的错误函数调用。
添加配置项crazyping,当其不为零时会在每次取网卡信息时进行ping操作。
--V1.11 --
20151218
添加在程序开始时对IPC变量是否已存在的判断如果已存在则将其删除。
将宏TESTINTERVAL的定义去除不然不会调用南瑞的库函数。
--V1.10 --
20151211
修改IPC信号量的相关操作
1)通过对代码的分析发现在使用信号量时存在错误。在使用进行PV原语操作时进行了相反的操作其中加锁操作应减少信号量的计数而去锁应增加信号量的计数但库中进行了相反的操作对其进行了修改。即将get_sem中对信号量的操作改为1而在release_sem函数中对信号量的操作变为1即+1。同时在init_sem函数中将该信号量的值初始化为1。
2信号量应由网卡监控程序sys_nicmonitor创建而不应由用户程序创建对其进行了相应的修改在创建信号量时在flag中添加IPC_EXCL,并将用户程序引用信号量时的flag中去除IPC_CREATE)。
3)在对获取网卡信息函数get_nic_info中添加获取时间的操作后得知其中对getpwdnam的调用会减慢函数的工作该函数并非每次都减慢函数很多多数在3040ms左右但在测试中曾达到2s左右。对其修改为在sys_nicmonitor程序启动时调用getpwdnam函数并将得到的用户主目录名写入到一块共享内存中。当用户程序要获得该目录名时则从该值从共享内存中读取出来而不需要每次都通过getpwdnam来获取故可以提高该函数的速度。
=-======update 2015-11-17
编译前进入dep目录运行sh run.sh安装编译依赖文件.
然后sh build.sh编译1.9版本的sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz
调试模式下:
cd sys_nicmonitor-1.9
make clean
make
make test
make runtest
版本号变更方法更新sys_nicmonitor/version.h文件需要同时修改//后面的和define中的前者用于pkg包后者用于sys_nicmonitor程序.
制作pkg包方法 sh build.sh
=========
编译前准备:
编译前先安装dotconf软件包。
libman.so拷贝到/usr/lib64/下
pkgadd dotconf#1.0.13-1.pkg.tar.gz
编译说明:
在Rocky 4.2上编译
执行: sh build.sh
将在当前的目录下生成安装包:
sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz
========
安装说明:
1)安装以root的用户执行安装命令
pkgadd sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz
如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装
卸载命令: pkgrm sys_nicmonitor
更新方式安装命令:
pkgadd u sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz
2查看安装是否成功
pkginfo -i|grep sys_nicmonitor
或者
查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否匹配
=======
版本说明:
--V1.9 --
2013-08-12
1修复问题占用内存快速累加导致内存占用很大
问题描述:日志线程资源退出时未回收,导致占用内存快速累加
解决办法:修改为一个日志线程,并且线程退出时设置为自动回收。
将日志写文件的地方放到common.c中以便动态库和daemon程序使用一个。
2修复问题日志文件名在家目录或者var目录下未在规定位置
问题描述日志文件命名在一个单独的线程进行导致日志线程和该线程对log_path存在同时访问的情况。
导致在换天或者换月时,日志文件名变更一半时,日志写线程此时写日志,用未更新完成的
log_path创建日志导致日志文件不再规定的位置。
解决办法增加线程互斥锁pthread_mutex对log_path进行保护修复该问题。
--V1.8.1--
2013-06-05
1)修改BUGping_gw函数当PING失败时没有尝试对余下的GW进行PING操作
2)增加环境变量SYS_NIC_DEBUG用于调试流量抖动问题。
--V1.8 --
20130527
对系统高磁盘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安装包

View File

@@ -0,0 +1,182 @@
#include <sys/stat.h>
#include <sys/types.h>
static pthread_mutex_t lock_record_logpath = PTHREAD_MUTEX_INITIALIZER;
#define LOGPATH_LOCK pthread_mutex_lock(&lock_record_logpath)
#define LOGPATH_UNLOCK pthread_mutex_unlock(&lock_record_logpath)
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)
#define MAX_LOG_COUNT 5000
static char **log_prepare = NULL;
static char **log_writing = NULL;
static int log_thread_exit = 1;
static int log_idx = 0;
void create_dir(char *create_path)
{
char tmp_path[256];
strcpy(tmp_path,create_path);
int i,len = strlen(tmp_path);
for(i=1; i<len; i++)
{
if(tmp_path[i]=='/')
{
tmp_path[i] = 0;
if( access(tmp_path, F_OK)!=0 )
{
if(mkdir(tmp_path, 0755)==-1)
{
perror("mkdir error");
}
}
tmp_path[i] = '/';
}
}
}
static void *record_log_thread( void *arg )
{
char *log_str;
FILE *log_fp = NULL;
char **tmp = NULL;
char tmp_path[256];
int i = 0;
LOGPATH_LOCK;
strcpy(tmp_path,log_path);
LOGPATH_UNLOCK;
// create_dir( log_path );
create_dir( tmp_path );
log_thread_exit = 0;
// log_fp = fopen(log_path, "a");
while( !log_thread_exit ) {
LOGPATH_LOCK;
strcpy(tmp_path,log_path);
LOGPATH_UNLOCK;
if( !log_fp ) {
// LOGPATH_LOCK;
// log_fp = fopen(log_path, "a");
log_fp = fopen(tmp_path, "a");
// LOGPATH_UNLOCK;
}
//prepare log strings
LOG_LOCK;
tmp = log_writing;
log_writing = log_prepare;
// log_prepare = log_writing;
log_prepare = tmp;
log_idx=0;
LOG_UNLOCK;
i = 0;
while( i < MAX_LOG_COUNT ) {
log_str = log_writing[i];
if( log_str == NULL ) {
break;
}
if( log_fp ) {
// fwrite( log_str, 1, strlen(log_str), log_fp ); //write to file
fwrite( log_str, strlen(log_str), 1, log_fp );
}
free( log_str ); //free
log_writing[i] = NULL; //clear
//next
i++;
}
if(log_fp) {
fclose(log_fp);
log_fp = NULL;
}
usleep(799999);
} //while
if( log_fp ) {
fclose(log_fp);
}
if( log_prepare ) free( log_prepare );
if( log_writing ) free( log_writing );
log_prepare = NULL;
log_writing = NULL;
//free self thread resources
pthread_detach(pthread_self());
return NULL;
}
/* write error in logfile */
void record_log(char *str)
{
static int log_thread_started = 0;
time_t tamp;
char str_tm[4];
char log_str[512];
struct tm tmptr;
struct timeval tv;
struct timezone tz;
pthread_t pid;
// int ret = 0;
char *keepstr = 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';
// ret = strftime(log_str, sizeof(log_str), "%F %T. ", &tmptr );
// snprintf(log_str+ret, sizeof(log_str)-ret, " %s %s %s", str_tm, process_name, str );
//
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);
//check log thread
if( log_thread_started == 0 ) {
log_thread_started = 1;
log_prepare = (char **)malloc( sizeof(char *)*MAX_LOG_COUNT );
log_writing = (char **)malloc( sizeof(char *)*MAX_LOG_COUNT );
memset( log_prepare, 0, sizeof(char *) *MAX_LOG_COUNT );
memset( log_writing, 0, sizeof(char *) *MAX_LOG_COUNT );
log_idx = 0;
pthread_create( &pid, NULL, record_log_thread, (void *)NULL );
}
LOG_LOCK;
if(log_idx < MAX_LOG_COUNT){
keepstr = strdup( log_str );
if(!keepstr){
LOG_UNLOCK;
return;
}
//LOG_LOCK;
if( log_prepare ) {
log_prepare[log_idx] = keepstr;
log_idx++;
}
}
LOG_UNLOCK;
}

View File

@@ -0,0 +1,51 @@
#ifndef _RTE_CONST_H
#define _RTE_CONST_H
#include <pthread.h>
#define MAX_STRING_LEN 24
#define MAX_EXECMD_LEN 200
#define MAX_FILENAME_LEN 200
#define MAX_CONTEXT 8
#define MAX_HOSTNAME_LEN 24
#define MAX_LOCAL_MESPROC 512
#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC
#define MAX_LOCAL_APP 32
#define MAX_LOCAL_NODE 256
#define MAX_SET 256 //max num of event set
#define MAX_EVENT 1300
#define MAX_REG_PROC 20
#define LEN_SHMBLK_BIG 4096
#define MAX_MSGBUF_LEN 32767
#define FREE_PAGE_SIZE 65536
#define MAX_PAGE_NUM 1024
#define MAX_QUE MAX_LOCAL_MESPROC
#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2
#define RTE_HAN 0 // queue number for event handeler0(RTE)
#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler
#define DOMAIN_I 1
#define DOMAIN_II 2
#define DOMAIN_III 3
//Add 20090225
const int PROC_TYPE_RPT =1;
const int PROC_TYPE_UNRPT =0;
//add end
//status for proc and app
const int NON_ACTIVE = 0;
const int ACTIVE = 1;
const int HANGUP = 2;
const int FAILURE = 5;
const int START = 6;
const int STOP = 7;
#endif

1825
code/sys_nicmonitor/mnic.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
#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 <arpa/inet.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include "sys_netcard.h"
#include <pwd.h>
#include <sys/types.h>
#define IPSIZE 16
char *get_name(char *, char *);
int if_fetch(NETCARD_INFO *);
int ioc_get_name(NETCARD_INFO *);
int get_dev_fields(char *p, NETCARD_INFO *);
int get_user_home(int , char *);
void send_alarm(D5000_NIC_ALARM *, int , char *);
void record_log(char *);
#endif

View File

@@ -0,0 +1,410 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>
#include <pthread.h>
#include "nicinfo_shm.h"
#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{
NETCARD_INFO info[MAXNICNUM];
}SHM;
static int semid = 0;
char log_path[1024];
char shm_path[1024];
char sem_path[1024];
SHM *ptr = NULL;
static char process_name[32] = "get_nic_info";
static void linx_free_shm( void *addr, int size );
#if 0
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;
}
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];
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);
}
#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
}
#else
#include "common.c"
#endif
void get_sem(int semid)
{
char err_str[200];
struct sembuf lock[2];
lock[0].sem_num = 0;
lock[0].sem_op = -1;
lock[0].sem_flg = SEM_UNDO;
/* lock[1].sem_num = 0;
lock[1].sem_op = 1;
lock[1].sem_flg = SEM_UNDO;*/
while(semop(semid, lock, 1)){
if (errno == EINTR) {
continue;
}
snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno));
record_log(err_str);
return;
}
}
void release_sem(int semid)
{
int ret;
char err_str[200];
struct sembuf unlock;
unlock.sem_num = 0;
unlock.sem_op = 1;
unlock.sem_flg = SEM_UNDO;
if((ret = semop(semid, &unlock, 1)) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno));
record_log(err_str);
return;
}
}
static int global_share_id = -1;
static void *global_shmptr = NULL;
int init_nic_info(void)
{
int fd = -1;
int sem_val;
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, 0666)) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno));
record_log(err_str);
return -1;
}
/*
if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno));
record_log(err_str);
semctl(semid, 0, IPC_RMID);
return -1;
}
if(!sem_val){
if(semctl(semid, 0, SETVAL, 1) == -1){
snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno));
record_log(err_str);
semctl(semid, 0, IPC_RMID);
return -1;
}
}
*/
#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);
return -1;
}
ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0);
if(ptr == MAP_FAILED){
snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno));
record_log(err_str);
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, 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;
time_t tamp;
struct tm tmptr;
char *s;
char buf[128];
char err_str[200];
struct passwd *user;
int shm_id;
int shm_key = 0x1d6010;
char *shm_buf;
#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);
if(log_path[0]==0){
shm_id = shmget(shm_key,128,0666);
if(shm_id < 0) return -1;
shm_buf = (char *)shmat(shm_id,NULL,0);
if(shm_buf == (void *) -1) return -1;
sprintf(log_path,"%s",shm_buf);
sprintf(shm_path,"%s",shm_buf);
sprintf(sem_path,"%s",shm_buf);
shmdt(shm_buf);
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
memset(buf, 0, sizeof(buf));
strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr);
strcat(buf, "sys_nicmonitor.log");
LOGPATH_LOCK;
s = strrchr(log_path, '/');
s++;
*s = '\0';
strcat(log_path, buf);
LOGPATH_UNLOCK;
if((ret = init_nic_info()) == -1){
return -1;
}
get_sem(semid);
for(i = 0; ptr->info[i].charname[0] != 0; i++){
if(!strcmp(ptr->info[i].charname, nic_name)){
// 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;
// }
}
}

View File

@@ -0,0 +1,52 @@
#ifndef NIC_SHM_H
#define NIC_SHM_H
#include <sys/socket.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef NIC_NAME_LEN
#define NIC_NAME_LEN 64
#endif
#define SEM_PROJ_ID 's'
#define MAXNICNUM 32
typedef long KEYID;
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;
unsigned long long 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;
int get_nic_info(char *nic_name, NETCARD_INFO *net_info);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,198 @@
////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved.
//
// FileName : procconf.h
//
// Function : this class realize some basic functions for process managerment,
// such as initiate process, report status of process, check status of process,
// update status of process, get information of process
//
// Author :
//
// Date :
//
// Modify by :
//
// Mod Date :
//
////////////////////////////////////////////////////////////////////////////////////
#ifndef _PROCCONF_H
#define _PROCCONF_H
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//#include <iostream.h>
#include <signal.h>
#include "const.h"
//for alarm ................................
struct MESS_BH
{
unsigned char mtype;
int length;
};
struct PROCESS_ALM
{
char context_name[MAX_STRING_LEN];
char app_name[MAX_STRING_LEN];
char proc_name[MAX_STRING_LEN];
unsigned char status;
};
//for alarm end .............................
//for mmi....................................
const int MAX_BUFFER_LEN = 500;
struct MESS_BLOCK
{
unsigned char num;
char buffer[MAX_BUFFER_LEN];
};
struct MESS_PROC
{
char context_name[MAX_STRING_LEN];
char app_name[MAX_STRING_LEN];
char proc_name[MAX_STRING_LEN];
char status;
};
//for mmi end ................................
const int DEFAULT_PERIOD = 3;
const int COUNT_LIMIT = 2;
const int START_DEFAULT_PERIOD = 60;
const int START_COUNT_LIMIT = 5;
const int APP_COUNT_LIMIT = 1;
//process critical level
//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed
//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed
//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed
//const int GENERAL = 4; // reboot the process
const int CRUCIAL = 1; // crucial process
const int GENERAL = 0; // general process
extern int srv_init(char *service,int port);
extern int Tcp_close(int sockfd);
extern int Tcp_read(int fd,char *ptr,int nbytes);
extern int Tcp_write(int fd,char *ptr,int nbytes);
extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen);
extern int client_tcp_open(char *host,char *service,int port);
typedef struct
{
char context_name[MAX_STRING_LEN];
char app_name[MAX_STRING_LEN];
char proc_name[MAX_STRING_LEN];
pid_t proc_pid;
}PROC_ADM_INFO;
//***************************************************************
// structure name : PROC_INFO
// function : store process informatin
// author :
// date :
// modify by :
// modification :
// mod date :
//***************************************************************
typedef struct
{
int position;
char node_name[MAX_STRING_LEN];
char context_name[MAX_STRING_LEN];
char app_name[MAX_STRING_LEN];
char proc_name[MAX_STRING_LEN];
unsigned char active_flag;
unsigned char master_flag;
time_t startup_time;
time_t refresh_time;
short refresh_peri;
unsigned char monitor_type;
pid_t proc_pid;
unsigned char auto_start;
unsigned char act_timer;
unsigned char start_timer;
unsigned char critical_level;
char exefile_path[MAX_EXECMD_LEN];
}PROC_INFO;
//***************************************************************
// structure name : APP_INFO
// function : store application informatin
// author :
// date :
// modify by :
// modification :
// mod date :
//***************************************************************
typedef struct
{
int position;
char context_name[MAX_STRING_LEN];
int context_id;
char app_name[MAX_STRING_LEN];
int app_id;
unsigned char act_timer;
unsigned char active_flag;
}APP_INFO;
typedef struct
{
int no_proc;
int semdes_cfg;
PROC_INFO proc[MAX_LOCAL_PROCESS];
APP_INFO app[MAX_LOCAL_APP];
}PROCCFG;
class proc_invocation
{
public:
int m_init;
PROCCFG *proccfg_p;
public:
proc_invocation();
~proc_invocation();
int conf_create();
//int check_proc_status();
//int update_rtdb();
//int kill_proc(pid_t pid);
//int start_proc(char *cmdline);
//int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status);
//int update_proc_status(char *context_name, char *app_name, char *proc_name, char status);
//int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start
int proc_init(char *context_name, char *app_name, char *proc_name);
int proc_report(int pos, char status, int intertime=3);
int proc_exit(int proc_pos);
int get_pos(char *context_name, char *app_name, char *proc_name);
int is_proc_exist(char *context_name, char *app_name, char *proc_name);
int conf_map();
int get_procinfo(int position, PROC_ADM_INFO *p_info);
int get_active_pid(int &num, int *p_pidlist);
int is_proc_run(pid_t pid);
int is_proc_run(char *context_name, char *app_name, char *proc_name);
};
#endif

View File

@@ -0,0 +1,142 @@
#include "mnic.h"
static int sys_nic_debug=0;
static char *prase_digit(char **s)
{
char *retp = NULL;
char *p = *s;
if(!p)return NULL;
while(!isdigit(*p)){
p++;
if(*p == '\0')return NULL;
}
retp = p;
while(isdigit(*p)){
p++;
if(*p == '\0'){
*s = NULL;
return retp;
}
}
*p = '\0';
p++;
*s = p;
return retp;
}
char *get_name(char *name, char *p)
{
while (isspace(*p))
p++;
while (*p) {
if (isspace(*p))
break;
if (*p == ':') { /* could be an alias */
char *dot = p, *dotname = name;
*name++ = *p++;
while (isdigit(*p))
*name++ = *p++;
if (*p != ':') { /* it wasn't, backup */
p = dot;
name = dotname;
}
if (*p == '\0')
return NULL;
p++;
break;
}
*name++ = *p++;
}
*name++ = '\0';
return p;
}
int get_dev_fields(char *str, NETCARD_INFO *ife)
{
if( getenv("SYS_NIC_DEBUG") ) {
sys_nic_debug = 1;
}
int i = 0;
char *retp[16];
char error_str[1024];
while((retp[i] = prase_digit(&str)) != NULL){i++;if(i > 15)break;}
ife->rx_bytes = atoll(retp[0]);
ife->rx_packets = atoll(retp[1]);
ife->rx_errors = atol(retp[2]);
ife->rx_dropped = atol(retp[3]);
ife->rx_fifo_errors = atol(retp[4]);
ife->rx_multicast = atol(retp[7]);
ife->tx_bytes = atoll(retp[8]);
ife->tx_packets = atoll(retp[9]);
ife->tx_errors = atol(retp[10]);
ife->tx_dropped = atol(retp[11]);
ife->tx_fifo_errors = atol(retp[12]);
ife->collisions = atol(retp[13]);
ife->tx_carrier_errors = atol(retp[14]);
if( 0 ) {
snprintf(error_str, sizeof(error_str), "ife->rx_bytes%lld:\tife->tx_bytes=%lld\n",ife->rx_bytes,ife->tx_bytes);
record_log(error_str);
}
return 0;
}
int if_fetch(NETCARD_INFO *ife)
{
int skfd;
struct ifreq ifr;
if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1;
strcpy(ifr.ifr_name, ife->charname);
if (!ioctl(skfd, SIOCGIFFLAGS, &ifr))
ife->flags = ifr.ifr_flags;
if (!ioctl(skfd, SIOCGIFHWADDR, &ifr))
ife->hwaddr = ifr.ifr_hwaddr;
if (!ioctl(skfd, SIOCGIFMTU, &ifr))
ife->mtu = ifr.ifr_mtu;
if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr))
ife->tx_queue_len = ifr.ifr_qlen;
if (!ioctl(skfd, SIOCGIFADDR, &ifr))
ife->addr = ifr.ifr_addr;
if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr))
ife->broadaddr = ifr.ifr_broadaddr;
if (!ioctl(skfd, SIOCGIFNETMASK, &ifr))
ife->netmask = ifr.ifr_netmask;
ife->time_stamp = time(NULL);
close(skfd);
return 0;
}
int ioc_get_name(NETCARD_INFO *name)
{
int skfd;
int ifrnum;
int i = 0;
struct ifconf ifc;
struct ifreq buf[MAXNICNUM];
if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1;
memset(buf, 0, sizeof(buf));
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
ioctl(skfd, SIOCGIFCONF, &ifc);
ifrnum = ifc.ifc_len / sizeof(struct ifreq);
while(ifrnum-- > 0){
strcpy(name[i].charname, buf[ifrnum].ifr_name);
i++;
}
close(skfd);
return i;
}

View File

@@ -0,0 +1,37 @@
#include "mnic.h"
void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4)
{
int bytes;
int sock_sd;
int val;
char error_str[200];
struct sockaddr_in recever;
socklen_t sock_len;
if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno));
record_log(error_str);
return;
}
val=1;
if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) {
snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno));
record_log(error_str);
return;
}
/* init socket*/
recever.sin_family = AF_INET;
recever.sin_port = htons(socket_port);
inet_pton(AF_INET, ipv4, &recever.sin_addr);
sock_len = sizeof(recever);
/* send alarm*/
if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){
snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno));
record_log(error_str);
return;
}
//snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n");
//record_log(error_str);
close(sock_sd);
}

View File

@@ -0,0 +1,88 @@
#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;
unsigned long long 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
#define NETCARD_ALARM_NORMAL 5
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; //012 重发次数
}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

View File

@@ -0,0 +1,44 @@
#include <stdio.h>
#include <fcntl.h>
#include <sys/time.h>
#include <string.h>
#include "nicinfo_shm.h"
int main(int argc, char *argv[])
{
NETCARD_INFO ni;
int ret = 0;
char dev[10];
struct timeval st,et;
unsigned long uset, stl, etl, to;
if( argc >=2 ) strncpy(dev,argv[1],9);
else strcpy(dev, "eth0");
if( argc >=3 ) to = strtol(argv[2], NULL, 10 );
else to= 500000ull;
while(1) {
gettimeofday(&st, NULL);
ret = get_nic_info( dev, &ni );
gettimeofday(&et, NULL);
stl = st.tv_sec;
stl = stl*1000000 + st.tv_usec;
etl = et.tv_sec;
etl = etl*1000000 + et.tv_usec;
uset = etl - stl;
// printf("%d, %x, %x\n", ret, uset, to);
if( ret != 0 || uset > to) {
///home/d5000/var/log/netcard/testnic.log
char buf[120];
int log = open("/home/d5000/var/log/netcard/testnic.log", O_CREAT|O_RDWR|O_APPEND, 0666);
snprintf(buf,sizeof(buf),"ret:%d, %lld-%lld=%lld\n", ret, etl, stl, uset);
write(log, buf, strlen(buf));
close(log);
}
// sleep(1);
}
}

View File

@@ -0,0 +1,181 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "sys_netcard.h"
#define IPSIZE 16
#define THRNR 10
#define DAYS 25
#define LINE_SIZE 2048
#define HOST_NAME_SIZE 40
#define BOND_PATH "/proc/net/bonding/*"
#define LOG_PATH "/var/log/netcard/"
#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.9"
#define NIC_UNKNOWN 0
#define NIC_DOWN 1
#define NIC_UNRUNNING 2
#define NIC_UNLINKABLE 3
#define NIC_NORMAL 4
//网卡设备描述结构
typedef struct __nic_dev{
char dev_name[DEV_MAXLEN]; //网卡名称
char ping_ip[MAX_GW][IPLEN]; //ping地址列表最多32个
int gw_num; //地址列表的长度
}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];
NIC_DEV nic[MAXNICNUM]; //被监视网卡的列表
char ip[IPSIZE];
int pingnum; //OPTIONAL!! How many times for once ping check? Default is 2.
int pinglap; //OPTIONAL!! How many seconds does ping wait for reply package? Default is 1.
int crazyping;
}CONFIG_FILE_ST;
typedef struct inc_name_node{
char name[NIC_NAME_LEN];
struct inc_name_node *next;
}NET_NAME_ST;
typedef struct inc_info_node{
NETCARD_INFO info;
int status;
struct inc_info_node *next;
}NETCARD_INFO_ST;
typedef struct net_info{
NETCARD_INFO info[MAXNICNUM];
}SHM;
typedef struct __thread_env_st{
D5000_NIC_ALARM Malarm;
char bond_file_path[64];
char host_name[HOST_NAME_SIZE];
int host_name_size;
CONFIG_FILE_ST *conf;
}THENV;
typedef struct __thread_mem_st{
int semid;
SHM *shm_ptr;
NETCARD_INFO_ST *listp;
CONFIG_FILE_ST *conf;
}THMEM;
typedef struct __thread_flow_st{
THENV env;
THMEM mem;
}THFLOW;
int sys_nic_debug = 1;
static void get_inc_stats(NETCARD_INFO *net);
int get_dev_fields(char *str, NETCARD_INFO *ife);
static char *prase_digit(char **s);
static void get_inc_stats(NETCARD_INFO *net)
{
char linebuf[LINE_SIZE];
char *retp = NULL;
char *str = NULL;
FILE *dev_fp;
if((dev_fp = fopen("/proc/net/dev", "r")) == NULL)return;
memset(linebuf, 0, sizeof(linebuf));
while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){
// printf("linebuf: %s charname: %s\n", linebuf,net->charname);
if((str = strstr(linebuf, net->charname)) != NULL){
str +=strlen(net->charname) + 2;
get_dev_fields(str, net);
break;
}
}
fclose(dev_fp);
}
static char *prase_digit(char **s)
{
char *retp = NULL;
char *p = *s;
if(!p)return NULL;
while(!isdigit(*p)){
p++;
if(*p == '\0')return NULL;
}
retp = p;
while(isdigit(*p)){
p++;
if(*p == '\0'){
*s = NULL;
return retp;
}
}
*p = '\0';
p++;
*s = p;
return retp;
}
int get_dev_fields(char *str, NETCARD_INFO *ife)
{
if( getenv("SYS_NIC_DEBUG") ) {
sys_nic_debug = 1;
}
fprintf(stdout, "str:%s\n",str);
int i = 0;
char *retp[16];
while((retp[i] = prase_digit(&str)) != NULL){i++;if(i > 15)break;}
ife->rx_bytes = atoll(retp[0]);
ife->rx_packets = atoll(retp[1]);
ife->rx_errors = atol(retp[2]);
ife->rx_dropped = atol(retp[3]);
ife->rx_fifo_errors = atol(retp[4]);
ife->rx_multicast = atol(retp[7]);
ife->tx_bytes = atoll(retp[8]);
ife->tx_packets = atoll(retp[9]);
ife->tx_errors = atol(retp[10]);
ife->tx_dropped = atol(retp[11]);
ife->tx_fifo_errors = atol(retp[12]);
ife->collisions = atol(retp[13]);
ife->tx_carrier_errors = atol(retp[14]);
if( sys_nic_debug ) {
fprintf(stdout, "ife->rx_bytes:\t%lld\tife->tx_bytes:\t%lld\n",ife->rx_bytes,ife->tx_bytes);
}
return 0;
}
int main (int argc, char *argv[]){
THMEM mem;
NETCARD_INFO_ST *curr;
NETCARD_INFO net;
sprintf(net.charname,"%s","bond0");
while (1){
get_inc_stats(&net);
printf("ifname:%s, tx:%llu rx:%llu\n",net.charname, net.tx_bytes, net.rx_bytes);
// flush(NULL);
sleep(1);
}
}

View File

@@ -0,0 +1,5 @@
// version=1.14
#ifndef MNIC_VERSION
#define MNIC_VERSION "1.20"
#endif