PracticeDev/study_clang/Mimic/monitor_system_status/monitor.cpp

377 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ===============================================================================================
*
* Filename: monitor.cpp
*
* Description: Monitor the operating status of the system and publish it.
*
* Version: 0.1
* Created: 2020年07月15日 10时21分
* Revision: none
* Compiler: gcc/g++
* Link: -lpthread
*
* Author: songtianlun, songtianlun@comleader.com.cn
* Organization: IEUCD
*
* ===============================================================================================*/
#include "monitor.h"
int main (void)
{
int key_process_ret, key_sys_value_ret, key_files_ret, key_log_ret;
pthread_t key_process_pth, key_sys_value_pth, key_files_pth, key_log_pth; //线程ID变量
init_config("Config.ini");
// 参数创建的线程ID线程参数调用函数函数参数
key_process_ret = pthread_create(&key_process_pth,NULL,monitor_key_process,NULL);
key_sys_value_ret = pthread_create(&key_sys_value_pth,NULL,monitor_key_sys_value,NULL);
key_files_ret = pthread_create(&key_files_pth,NULL,monitor_key_files,NULL);
key_log_ret = pthread_create(&key_log_pth,NULL,monitor_key_log,NULL);
pthread_join(key_process_pth, NULL); // 等待线程结束
pthread_join(key_sys_value_pth, NULL);
pthread_join(key_files_pth, NULL);
pthread_join(key_log_pth, NULL);
return 0;
}
void *monitor_key_process(void *args)
{
printf("Start the new thread to monitor key process.\n");
}
void *monitor_key_sys_value(void *args)
{
Cpu_info ocpu,ncpu;
Mem_info omem;
User_info users[50];
Group_info groups[50];
char crontabs[100][200];
float cpu_occupy = 0;
float mem_occupy = 0;
int sum_tcp_socket = 0;
int sum_progress = 0;
int sum_user = 0;
int sum_group = 0;
int sum_crontab = 0;
memset(&users,0,sizeof(users));
printf("Start the new thread to monitor key system values.\n");
while(1)
{
// printf("-------------------- CPU occupy -------------------\n");
get_cpu_occupy(&ocpu);
sleep(1);
get_cpu_occupy(&ncpu);
cpu_occupy = cal_cpu_occupy(&ocpu, &ncpu);
// printf("-------------------- Mem occupy -------------------\n");
get_mem_occupy(&omem);
mem_occupy = cal_mem_occupy(&omem);
sum_tcp_socket = get_tcp_socket_sum();
sum_progress = get_progress_sum();
sum_user = get_users(users);
sum_group = get_groups(groups);
sum_crontab = get_crontab(crontabs);
// system("clear");
printf("-------------------- Monitor Info -------------------\n");
print_now_time();
printf("CPU Usage(%) : %4.2f\n", cpu_occupy);
printf("Mem Usage(%) : %4.2f\n", mem_occupy);
printf("Tcp Socket Establish : %d\n", sum_tcp_socket);
printf("Sum of progress : %d\n", sum_progress);
printf("Sum of Users : %d\n", sum_user);
printf("Sum of Groups : %d\n", sum_group);
printf("Sum of Crontabs : %d\n", sum_crontab);
printf("--------------------- User List --------------------\n");
for(int i=0;i<sum_user;i++)
{
printf("user %d name=%s, user id=%s, user group id=%s\n",i, users[i].name,users[i].cid,users[i].cgroup_id);
}
printf("-------------------- Group List --------------------\n");
for(int i=0;i<sum_group;i++)
{
printf("group name=%s, group id=%s\n",groups[i].name,groups[i].gid);
}
printf("------------------- Crontab List -------------------\n");
for(int i=0;i<sum_crontab;i++)
{
printf("%s",crontabs[i]);
}
printf("\n");
}
}
void *monitor_key_files(void *args)
{
printf("Start the new thread to monitor key files.\n");
}
void *monitor_key_log(void *args)
{
printf("Start the new thread to monitor key logs.\n");
}
int init_conf(char *conf_path)
{
}
void get_cpu_occupy (Cpu_info *o)
{
FILE *fd;
char buff[MAXBUFSIZE];
fd = fopen ("/proc/stat", "r"); //这里只读取stat文件的第一行及cpu总信息如需获取每核cpu的使用情况请分析stat文件的接下来几行。
fgets (buff, sizeof(buff), fd);
// printf("get thr cpu info: %s", buff);
sscanf (buff, "%s %u %u %u %u %u %u %u %u %u %u", o->name, &o->user, &o->nice, &o->system, &o->idle, &o->iowait, &o->irq, &o->softirq, &o->stealstolen, &o->guest, &o->guest_nice);
// printf("name=%s, user=%d, nice=%d, system=%d, idle=%d, iowait=%d, irq=%d, softirq=%d, stralstolen=%d, guest=%d, guest_nice=%d\n", o->name, o->user, o->nice, o->system, o->idle, o->iowait, o->irq, o->softirq, o->stealstolen, o->guest, o->guest_nice);
fclose(fd);
}
float cal_cpu_occupy (Cpu_info *o, Cpu_info *n)
{
int ototal, ntotal;
int oused, nused;
ototal = (o->user + o->nice + o->system + o->idle + o->iowait + o-> irq + o-> softirq + o->stealstolen + o->guest + o->guest_nice);
ntotal = (n->user + n->nice + n->system + n->idle + n->iowait + n-> irq + n-> softirq + n->stealstolen + n->guest + n->guest_nice);
oused = ototal - o->idle;
nused = ntotal - n->idle;
// printf("ototal time: %d\n", ototal);
// printf("ntotal time: %d\n", ntotal);
// printf("oused time: %d\n", oused);
// printf("nused time: %d\n", nused);
return (100.0 * (nused - oused) / (ntotal - ototal));
}
void get_mem_occupy (Mem_info *o)
{
FILE* fpMemInfo = fopen("/proc/meminfo", "r");
if (NULL == fpMemInfo)
{
return ;
}
int i = 0;
int value;
char name[1024];
char line[1024];
int nFiledNumber = 2;
int nMemberNumber = 5;
while (fgets(line, sizeof(line) - 1, fpMemInfo))
{
if (sscanf(line, "%s%u", name, &value) != nFiledNumber)
{
continue;
}
if (0 == strcmp(name, "MemTotal:"))
{
++i;
o->total = value;
}
else if (0 == strcmp(name, "MemFree:"))
{
++i;
o->free = value;
}
else if (0 == strcmp(name, "MemAvailable:"))
{
++i;
o->available = value;
}
else if (0 == strcmp(name, "Buffers:"))
{
++i;
o->buffers = value;
}
else if (0 == strcmp(name, "Cached:"))
{
++i;
o->cached = value;
}
if (i == nMemberNumber)
{
break;
}
}
// system("free");
// system("cat /proc/meminfo");
// printf("MemTotal : %d\n",o->total);
// printf("MemFree : %d\n",o->free);
// printf("MemAvailable : %d\n",o->available);
// printf("MemBuffers : %d\n",o->buffers);
// printf("MemCached : %d\n",o->cached);
// printf("MemSwapCached : %d\n",o->swap_cached);
// printf("MemSwapTotal : %d\n",o->swap_total);
// printf("MemSwapFree : %d\n",o->swap_free);
fclose(fpMemInfo);
}
float cal_mem_occupy(Mem_info *o)
{
return (100.0 * (o->total - o->available) / o->total);
}
int get_tcp_socket_sum ()
{
int socket_num=0;
FILE *tcp;
char tmp[1024]; //设置一个合适的长度,以存储每一行输出
int value;
char name[1024];
tcp = popen("netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'", "r");
if (tcp == NULL) {
return 0;
}
while (fgets(tmp, sizeof(tmp) - 1, tcp)) {
if (sscanf(tmp, "%s %u", name, &value) != 2)
{
continue;
}
if (0 == strcmp(name, "ESTABLISHED"))
{
socket_num = value;
break;
}
}
pclose(tcp);
return socket_num;
}
int get_progress_sum ()
{
int progress_num=0;
FILE *progress;
char tmp[1024];
progress = popen("ps -ef| wc -l", "r");
if (progress == NULL) {
return 0;
}
fgets(tmp, sizeof(tmp) - 1, progress);
sscanf(tmp, "%u", &progress_num);
pclose(progress);
return progress_num;
}
int get_users (User_info users[])
{
char line[10240];
char *name = NULL;
char *passwd = NULL;
char *cid;
char *cgroup_id;
int i=0;
FILE* fpUserInfo = fopen("/etc/passwd", "r");
if (NULL == fpUserInfo)
{
return -1;
}
while (fgets(line, sizeof(line) - 1, fpUserInfo))
{
// printf("%s",line);
name = strtok(line, ":");
passwd = strtok(NULL, ":");
cid = strtok(NULL, ":");
cgroup_id = strtok(NULL, ":");
strcpy(users[i].name, name);
strcpy(users[i].passwd, passwd);
strcpy(users[i].cid,cid);
strcpy(users[i].cgroup_id,cgroup_id);
// printf("user name=%s, user id=%s, user group id=%s\n",users[i].name,users[i].cid,users[i].cgroup_id);
i++;
}
fclose(fpUserInfo);
return i;
}
int get_groups (Group_info groups[])
{
char line[10240];
char *name = NULL;
char *passwd = NULL;
char *gid;
int i=0;
FILE* fpGroupInfo = fopen("/etc/group", "r");
if (NULL == fpGroupInfo)
{
return -1;
}
while (fgets(line, sizeof(line) - 1, fpGroupInfo))
{
// printf("%s",line);
name = strtok(line, ":");
passwd = strtok(NULL, ":");
gid = strtok(NULL, ":");
strcpy(groups[i].name, name);
strcpy(groups[i].passwd, passwd);
strcpy(groups[i].gid,gid);
// printf("group name=%s, group id=%s\n",groups[i].name,groups[i].gid);
i++;
}
fclose(fpGroupInfo);
return i;
}
int get_crontab(char crontab[][200])
{
int sum_crontab=0;
FILE *fpCrontab;
char tmp[10240]; //设置一个合适的长度,以存储每一行输出
char info[1024];
fpCrontab = popen("cat /etc/passwd | cut -f 1 -d : |xargs -I {} crontab -l -u {}", "r");
if (fpCrontab == NULL) {
return -1;
}
while (fgets(tmp, sizeof(tmp) - 1, fpCrontab)) {
sscanf(tmp, "%[^ ]", info);
if(!strcmp(info,"no"))
continue;
strcpy(crontab[sum_crontab++],tmp);
}
pclose(fpCrontab);
return sum_crontab;
}
/*
* === FUNCTION ======================================================================
* Name: print_now_time
* Description: 获取并格式化打印当前时间。
* =====================================================================================
*/
void print_now_time()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [128];
time (&rawtime);
// printf("%ld\n", rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,sizeof(buffer),"Now is %Y/%m/%d %H:%M:%S",timeinfo);
printf("%s\n", buffer);
}
/*
* === FUNCTION ======================================================================
* Name: split
* Description: 将str字符以spl分割,存于dst中并返回子字符串数量
* =====================================================================================
*/
int split(char dst[][80], char* str, const char* spl)
{
int n = 0;
char *result = NULL;
result = strtok(str, spl);
while( result != NULL )
{
strcpy(dst[n++], result);
result = strtok(NULL, spl);
}
return n;
}