Linux 下监控程序 — 共享内存通讯,消息订阅发布
实习的时候写的小练习程序:
VMSTAT.h
此代码用以从服务器获取运行状态信息
class VMSTAT
{
public:
double stat[20];
/*
* Proc
*
* 0:r: The number of processes waiting for run time.
* 1:b: The number of processes in uninterruptible sleep.
*
* Memory
*
* 2:swpd: the amount of virtual memory used.
* 3:free: the amount of idle memory.
* 4:buff: the amount of memory used as buffers.
* 5:cache: the amount of memory used as cache.
*
* Swap
*
* 6:si: Amount of memory swapped in from disk (/s).
* 7:Amount of memory swapped to disk (/s).
*
* IO
*
* 8:bi: Blocks received from a block device (blocks/s).
* 9:bo: Blocks sent to a block device (blocks/s).
*
* System
*
* 10:in: The number of interrupts per second, including the clock.
* 11:cs: The number of context switches per second.
*
* CPU
*
* 12:us: Time spent running non-kernel code. (user time, including nice time)
* 13:sy: Time spent running kernel code. (system time)
* 14:id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
* 15:wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
* 16:st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
*
* Add
*
* 17:Total Memory
* 18:Used Memory
* 19:CPU Usage
*/
void update()
{
//init array
FILE *file = popen("vmstat", "r");
char buf[300];
fgets(buf, 300, file);
fgets(buf, 300, file);
fgets(buf, 300, file);
int index = 0;
for (int i = 0; i < 300; i++)
{
while (buf[i] < '0' || buf[i] > '9')
i++;
int tmp = 0;
while (buf[i] >= '0' && buf[i] <= '9')
{
tmp = tmp * 10 + (buf[i] - '0');
i++;
}
stat[index++] = tmp * 1.0;
if (index == 17)
break;
}
pclose(file);
//total memory
file = popen("cat /proc/meminfo", "r");
double total = 0;
fgets(buf, 300, file);
char str[7];
for (int i = 17; i < 24; i++)
str[i - 17] = buf[i];
for (int i = 0; i < 7; i++)
{
total = total * 10 + (str[i] == ' ' ? 0 : (str[i] - '0'));
}
//calculate all
stat[17] = total;
stat[18] = total - stat[3];
stat[19] = 100 - stat[14];
pclose(file);
}
};
pshm.h
定义共享内存数据结构的结构体
定义主题ID
typedef struct tagMyStat
{
int attche_count;
int total;
int avaliable;
int used;
int cpu_useage;
} MyStat;
#define CENTOS_PER (4499) //主题CENTOS_PER 由服务器发布 客户端进行订阅 服务器可发布多个主题,而客户端可以选择其中若干个感兴趣的进行订阅
shmClient.cpp
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include "VMSTAT.h"
#include "pshm.h"
int main()
{
void *shm = NULL;
MyStat *shared = NULL;
int shmid = shmget((key_t) CENTOS_PER, sizeof(MyStat), IPC_CREAT);
if (shmid == -1)
{
printf("%s\n", "shmget failed!");
return 0;
}
shm = shmat(shmid, (void*) 0, 0);
if (shm == (void*) -1)
{
printf("%s\n", "shmat failed!");
}
printf("Memory attached at %lx\n", (long) shm);
shared = (MyStat*) shm;
shared->attche_count++;
time_t rawtime;
struct tm *t;
while (1)
{
time(&rawtime);
t = localtime(&rawtime);
printf(
"Client@%d:%d:%d :Total: %d MB Aviliable: %d MB Used: %d MB CPU: %d %\n",
t->tm_hour, t->tm_min, t->tm_sec, shared->total,
shared->avaliable, shared->used, shared->cpu_useage);
sleep(1);
}
shared->attche_count--;
if (shmdt(shm) == -1)
{
printf("%s\n", "shmdt failed!");
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
printf("%s\n", "shmctl(IPC_RMID) failed!");
}
sleep(2);
return 0;
}
shmServer.cpp
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include "VMSTAT.h"
#include "pshm.h"
int main()
{
void *shm = NULL;
MyStat *shared = NULL;
int shmid = shmget((key_t) CENTOS_PER, sizeof(MyStat), 0666 | IPC_CREAT);
if (shmid == -1)
{
printf("%s\n", "shmget failed!");
return 0;
}
shm = shmat(shmid, (void*) 0, 0);
if (shm == (void*) -1)
{
printf("%s\n", "shmat failed!");
}
printf("Memory attached at %lx\n", (long) shm);
shared = (MyStat*) shm;
memset(shared, 0, sizeof(MyStat));
shared->attche_count = 0;
VMSTAT s;
time_t rawtime;
struct tm *t;
while (1)
{
s.update();
shared->total = int(s.stat[17] / 1000);
shared->avaliable = int(s.stat[3] / 1000);
shared->used = int(s.stat[18] / 1000);
shared->cpu_useage = int(s.stat[19]);
time(&rawtime);
t = localtime(&rawtime);
printf(
"Server@%2d:%2d:%2d (%d):Total: %d MB Aviliable: %d MB Used: %d MB CPU: %d %\n",
t->tm_hour, t->tm_min, t->tm_sec, shared->attche_count,
shared->total, shared->avaliable, shared->used,
shared->cpu_useage);
sleep(1);
}
if (shmdt(shm) == -1)
{
printf("%s\n", "shmdt failed!");
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
printf("%s\n", "shmctl(IPC_RMID) failed!");
}
sleep(2);
return 0;
}
服务器运行截图:
客户端运行截图: