1. 版本1.9, 修复问题1)日志线程修改为1个线程,退出时回收线程资源;2)日志文件命名线程和日志写入线程对log_path存在访问冲突,导致日志文件创建位置存在不对的可能。3)common.c为daemon和so共同使用的日志线程写入的函数,提取出来方便维护

git-svn-id: http://172.17.0.253/svn/soft_pkgs/sys_nicmonitor@1415 09c3743a-b0dd-45d3-b506-aa74c7a2a6ef
This commit is contained in:
rbqiu
2013-08-14 05:30:41 +00:00
parent af5b85babe
commit c43857c794
14 changed files with 408 additions and 95 deletions

1
code/trunk/LATEST Normal file
View File

@@ -0,0 +1 @@
1.9

View File

@@ -1,6 +1,9 @@
=========
编译说明:
在Rocky 4.2上编译
编译前先安装dotconf软件包。
pkgadd dotconf#1.0.13-1.pkg.tar.gz
执行: sh build.sh
将在当前的目录下生成安装包:
sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz

View File

@@ -5,7 +5,7 @@
# Depends on: libart_lgpl libcgi libgd pango python tcl intltool
name=sys_nicmonitor
version=1.8.1
version=1.9
release=x86_64-Linx-Rocky4.2
workdir=`pwd`
#source=/home/d5000/d5000/linx/1.8/$name-$version.tar.gz

View File

@@ -1,27 +1,52 @@
=========
编译前准备:
编译前先安装dotconf软件包。
libman.so拷贝到/usr/lib64/下
pkgadd dotconf#1.0.13-1.pkg.tar.gz
编译说明:
在Rocky 4.2上编译
1修改Pkgfile中的source指向的源文件位置
2然后执行: sh build.sh
执行: sh build.sh
将在当前的目录下生成安装包:
sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz
========
安装说明:
1)安装以root的用户执行安装命令
pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz
pkgadd sys_nicmonitor#1.9-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
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的日期是否
查看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将可能会被阻塞。本版本为此作出调整。

View File

Binary file not shown.

View File

@@ -0,0 +1,90 @@
=========
编译前准备:
编译前先安装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,159 @@
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)
{
int i,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 void *record_log_thread( void *arg )
{
char *log_str;
FILE *log_fp = NULL;
char **tmp = NULL;
int i = 0;
create_dir( log_path );
log_thread_exit = 0;
// log_fp = fopen(log_path, "a");
while( !log_thread_exit ) {
if( !log_fp ) {
LOGPATH_LOCK;
log_fp = fopen(log_path, "a");
LOGPATH_UNLOCK;
}
//prepare log strings
LOG_LOCK;
tmp = log_writing;
log_writing = log_prepare;
log_prepare = log_writing;
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
}
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 );
}
keepstr = strdup( log_str );
LOG_LOCK;
if( log_prepare ) {
log_prepare[log_idx] = keepstr;
log_idx++;
}
LOG_UNLOCK;
}

View File

@@ -1,5 +1,6 @@
#include "mnic.h"
#include "dotconf.h"
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/ipc.h>
@@ -20,7 +21,7 @@
#define IPLEN 16
#define CONF_FILE "/conf/nic/sys_netcard_conf.txt"
#define MNIC_VERSION "1.8.1"
#define MNIC_VERSION "1.9"
#define NIC_UNKNOWN 0
#define NIC_DOWN 1
@@ -161,28 +162,72 @@ static int sys_nic_debug = 0;
void create_dir(char *create_path);
#if 0
///////////////// no use
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;
#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;
static void *record_log_thread( void *arg )
{
char *log_str = (char *)arg;
char *log_str;
FILE *log_fp = NULL;
char **tmp = NULL;
inti i = 0;
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){
create_dir( log_path );
log_thread_exit = 0;
log_fp = fopen(log_path, "a");
while( !log_thread_exit ) {
if( !log_fp ) {
log_fp = fopen(log_path, "a");
}
fclose(log_fp);
}
LOG_UNLOCK;
free(log_str);
//prepare log strings
LOG_LOCK;
tmp = log_writing;
log_writing = log_prepare;
log_prepare = log_writing;
log_idx=0;
LOG_UNLOCK;
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
}
free( log_str ); //free
log_writing[i] = NULL; //clear
//next
i++;
}
usleep(100);
} //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;
return NULL;
}
@@ -219,43 +264,54 @@ void record_log(char *str)
fclose(log_fp);
}
#else
static int log_thread_started = 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, 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);
}
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. %s %s %s", &tmptr, str_tm, process_name, 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_prepare ) {
log_prepare[log_idx] = keepstr;
log_idx++;
}
LOG_UNLOCK;
#endif
}
#else
#include "common.c"
#endif
static void init_sem(int *semid, CONFIG_FILE_ST *ptr)
{
key_t key;
@@ -908,6 +964,7 @@ static void *create_logfile(void *ptr)
else if(old_day != tmptr.tm_mday)flag = 1;
else flag = 0;
if(flag){
LOGPATH_LOCK;
s = strrchr(log_path, '/');
s++;
*s = '\0';
@@ -916,6 +973,7 @@ static void *create_logfile(void *ptr)
strcat(buf, process_name);
strcat(buf, ".log");
strcat(log_path, buf);
mode_t mask_tmp;
mask_tmp = umask(0000);
if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){
@@ -923,6 +981,8 @@ static void *create_logfile(void *ptr)
close(fd);
}
umask(mask_tmp);
LOGPATH_UNLOCK;
old_mon = tmptr.tm_mon+1;
old_day = tmptr.tm_mday;
strcpy(del_file[head++], log_path);
@@ -1246,29 +1306,6 @@ int get_broad_ip(void)
return 0;
}
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] = '/';
}
}
}
void create_semdir()
{
#if 0

View File

@@ -30,37 +30,15 @@ static char log_path[1024];
static char shm_path[1024];
static char sem_path[1024];
SHM *ptr = NULL;
static char process_name[32] = "get_nic_info";
static void linx_free_shm( void *addr, int size );
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] = '/';
}
}
}
#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 void linx_free_shm( void *addr, int size );
static int is_checkdir = 0;
static void *record_log_thread( void *arg )
@@ -152,6 +130,11 @@ void record_log(char *str)
}
#endif
}
#else
#include "common.c"
#endif
void get_sem(int semid)
{

View File

@@ -0,0 +1,15 @@
#include "nicinfo_shm.h"
int main(int argc, char *argv[])
{
NETCARD_INFO ni;
int ret = 0;
while(1) {
ret = get_nic_info( "eth0", &ni );
sleep(1);
}
}

Binary file not shown.