1. 模块关系图
2. 定时器模块 == 模拟定时器 + 基本定时器
模拟定时器结构
typedef struct stuSimTimer
{
char szName[20]; //定时器时间
int nInterval; //定时间隔,取0时表示取消定时
time_t nLast; //上次定时的系统时间
int nRemain; //剩余时间
int (*pfunc)(char *); //定时到达 执行代码
} SIMTIMER;
typedef SIMTIMER * PSIMTIMER;
模拟定时器有两种方法 :
a: 相对时间法
for (i=0; i<nTopTimer; i++)
{
sTimer[i].nRemain--;
if (sTimer[i].nRemain <= 0)
{
sTimer[i].pfunc(sTimer[i].szName);
sTimer[i].nRemain = sTimer[i].nInterval;
}
}
b: 绝对时间法
for (i=0; i<nTopTimer; i++)
{
if(time(NULL) - sTimer[i].nLast >= sTimer[i].nInterval)
{
sTimer[i].pfunc(sTimer[i].szName);
sTimer[i]. nLast = time(NULL);
}
}
基本定时器也有两种方法:
a: 循环定时
while(1)
{
sleep(1);
........
}
b:信号定时
void timefunc(int sig)
{
.....
signal(SIGALRM, timefunc);
alarm(n);
}
.......
定时器示例一:循环定时 + 绝对时间
#include<time.h>
#include<iostream>
using namespace std;
typedef struct stuSimTimer
{
char szName[20];
int nInterval;
time_t nLast;
int nRemain;
int (*pfunc)(char *);
} SIMTIMER;
typedef SIMTIMER * PSIMTIMER;
int func1(char *p)
{
cout << time(NULL) <<"Now Timer is: " << p << endl;
//PrintLog(stdout, "Now Timer : %s", p);
return 0;
}
#define MAXTIMER 20
SIMTIMER sTimer[MAXTIMER] =
{
{"TIMER1", 10, 0, 10, func1},
{"TIMER2", 4, 0, 4, func1}
};
int nTopTimer = 2;
int main()
{
int i;
while(1)
{
sleep(1);
for (i=0; i<nTopTimer; i++)
{
if(time(NULL) - sTimer[i].nLast >= sTimer[i].nInterval)
{
sTimer[i].pfunc(sTimer[i].szName);
sTimer[i]. nLast = time(NULL);
}
}
}
}
定时器示例二: 信号定时 + 相对时间
#include<stdio.h>
#include<time.h>
#include<iostream>
#include<signal.h>
using namespace std;
typedef struct stuSimTimer
{
char szName[20];
int nInterval;
time_t nLast;
int nRemain;
int (*pfunc)(char *);
} SIMTIMER;
typedef SIMTIMER * PSIMTIMER;
int func1(char *p)
{
//PrintLog(stdout, "Now Timer : %s", p);
cout << time(NULL) <<"Now Timer is: " << p << endl;
return 0;
}
#define MAXTIMER 20
SIMTIMER sTimer[MAXTIMER] =
{
{"TIME1", 10, 0, 10, func1},
{"TIMER2", 4, 0, 4, func1}
};
int nTopTimer = 2;
void timefunc(int sig)
{
int i;
for (i=0; i<nTopTimer; i++)
{
sTimer[i].nRemain--;
if (sTimer[i].nRemain <= 0)
{
sTimer[i].pfunc(sTimer[i].szName);
sTimer[i].nRemain = sTimer[i].nInterval;
}
}
signal(SIGALRM, timefunc);
alarm(1);
}
int main()
{
signal(SIGALRM, timefunc);
alarm(1);
cout << "----- Begin -------" << endl;
//PrintLog(stdout, "---Begin---");
while(1)
{
sleep(100);
}
}
3. 监控代码示例,消息队列,进程监控,文件系统监控。。。
#include <iostream>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/vfs.h>
using namespace std;
int GetMsg1(char *pHost) //监控有没有0x1234这个消息队列
{
char szBuf[256], szFmt[] = "%s|MSG|0x1234|%d|%d|%d|%s\n";
struct msqid_ds buf;
int msgid, unused;
float rate;
memset(&buf, 0, sizeof(buf));
if ((msgid = msgget(0x1234, 0666)) == -1 ||
msgctl(msgid, IPC_STAT, &buf) == -1)
{
printf(szFmt, pHost, 0, 0, 1, strerror(errno));
return 0;
}
unused = buf.msg_qbytes - buf.msg_cbytes;
rate = (unused*1.0) / buf.msg_qbytes;
memset(szBuf, 0, sizeof(szBuf));
sprintf(szBuf, "space: %d of %d available (%.2f%%).", unused, buf.msg_qbytes, rate*100);
if (rate >= 0.9)
{
printf(szFmt, pHost, unused , buf.msg_qbytes, 2, szBuf);
}
else
{
printf(szFmt, pHost, unused , buf.msg_qbytes, 0, szBuf);
}
}
int GetProcess1(char *pHost) //监控有没有monitor_type这个进程
{
int all = 0, off;
char szLine[256], szFmt[] = "%s|PROC|%s|%d|1|%d|%s\n";
char *ProcessName = "monitor_type";
char *ShellName = "ps -ef|grep monitor_type";
FILE *pfile;
char szUID[20], szPID[20], szPPID[20], szC[20], szSTIME[20], szTTY[20], szTIME[200], szCMD[255];
if ((pfile = popen(ShellName, "r")) == NULL)
{
printf(szFmt, ProcessName, pHost, 0, 1, "error:shell");
}
else
{
memset(szLine, 0, sizeof(szLine));
while (fgets(szLine, sizeof(szLine), pfile) != NULL)
{
sscanf(szLine, "%s%s%s%s%s%s%s%s", szUID, szPID, szPPID, szC, szSTIME, szTTY, szTIME,szCMD);
//cout << "szLine: " << szLine << endl;
//cout << "szCMD: " << szCMD << endl;
if (strlen(szCMD) >= strlen(ProcessName))
{
off = strlen(szCMD)-strlen(ProcessName);
if (strcmp(szCMD+off, ProcessName) == 0) all++;
}
}
pclose(pfile);
if (all > 0) printf(szFmt, pHost, ProcessName, all, 0, "OK");
else printf(szFmt, pHost, ProcessName, all, 1, "No Process");
}
}
int GetFilesysValue(char *pHost, char *pPath) //监控制定目录文件系统的信息
{
char szFmt[] = "%s|FILE|%s|%d|%d|%d|%s\n", szRate1[30], szRate2[30];
struct statfs buf;
float rused, rinode;
int nalarm;
if (statfs(pPath, &buf) == 0)
{
rused = (double)buf.f_bfree / buf.f_blocks;
rinode = (double)buf.f_ffree / buf.f_files;
sprintf(szRate1, "block : %.2f%% available", rused*100);
sprintf(szRate2, "inode : %.2f%% available", rinode*100);
if (rused >= 0.15) nalarm = 0;
else if (rused >= 0.5) nalarm = 2;
else nalarm = 1;
printf(szFmt, pHost, pPath, buf.f_bfree, buf.f_blocks, nalarm, szRate1);
if (rinode >= 0.15) nalarm = 0;
else if (rinode >= 0.5) nalarm = 2;
else nalarm = 1;
printf(szFmt, pHost, pPath, buf.f_ffree, buf.f_files, nalarm, szRate2);
}
return 0;
}
int main()
{
cout << "test monitor type ----> msg " << endl;
int msgid = msgget(0x1234, 0666 | IPC_CREAT);
msgsnd(msgid, "1", strlen("1"), 0);
GetMsg1("kevin_pc");
cout << "test monitor type ----> process " << endl;
GetProcess1("kevin_pc");
cout << "test monitor type ----> filesystem " << endl;
GetFilesysValue("kevin_pc", "/usr");
GetFilesysValue("kevin_pc", "/");
return 0;
}