199 lines
5.0 KiB
C
199 lines
5.0 KiB
C
|
/*
|
|||
|
* @Descripttion:
|
|||
|
* @version:
|
|||
|
* @Author: pengming
|
|||
|
* @Date: 2023-09-25 17:28:48
|
|||
|
* @LastEditors: pengming
|
|||
|
*/
|
|||
|
#pragma once
|
|||
|
#include <stdio.h>
|
|||
|
#include <sstream>
|
|||
|
#include <unistd.h>
|
|||
|
#include <sys/types.h>
|
|||
|
#include <sys/stat.h>
|
|||
|
#include <fcntl.h>
|
|||
|
#include <syslog.h>
|
|||
|
#include <sys/vfs.h>
|
|||
|
|
|||
|
#define ENLOG_SYSLOG
|
|||
|
#define VMRSS_LINE 21
|
|||
|
#define VMSIZE_LINE 17
|
|||
|
#define PROCESS_ITEM 29
|
|||
|
|
|||
|
struct _fileSystem_info{
|
|||
|
char fileSystem_format[8];
|
|||
|
unsigned int fileSystem_total_capacity;
|
|||
|
unsigned int fileSystem_free_capacity;
|
|||
|
char fileSystem_permissions[3];
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/*block to kbyte*/
|
|||
|
unsigned long u_kscale(unsigned long m_block, unsigned long m_kbyte)
|
|||
|
{
|
|||
|
|
|||
|
return ((unsigned long long)m_block * m_kbyte + 1024 / 2) / 1024;
|
|||
|
}
|
|||
|
|
|||
|
/*convert size to GB MB KB*/
|
|||
|
void convert_size(float m_size, int *dest)
|
|||
|
{
|
|||
|
if ((((m_size / 1024.0) / 1024.0)) >= 1.0)
|
|||
|
{
|
|||
|
*dest = (int)((m_size / 1024.0) / 1024.0);
|
|||
|
}
|
|||
|
else if ((m_size / 1024.0) >= 1.0)
|
|||
|
{
|
|||
|
*dest = (int)(m_size / 1024);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void writesyslog(int loglevel, std::string loginfo)
|
|||
|
{
|
|||
|
#ifdef ENLOG_SYSLOG
|
|||
|
openlog("scanservice", LOG_CONS | LOG_PID, LOG_USER);
|
|||
|
syslog(loglevel, "%s \n", loginfo.c_str());
|
|||
|
closelog();
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
unsigned int u_get_proc_virtualmem(unsigned int pid)
|
|||
|
{
|
|||
|
char file_name[64] = {0};
|
|||
|
FILE *fd;
|
|||
|
char line_buff[512] = {0};
|
|||
|
sprintf(file_name, "/proc/%d/status", pid);
|
|||
|
|
|||
|
fd = fopen(file_name, "r");
|
|||
|
if (nullptr == fd)
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
char name[64];
|
|||
|
int vmsize;
|
|||
|
for (int i = 0; i < VMSIZE_LINE - 1; i++)
|
|||
|
{
|
|||
|
fgets(line_buff, sizeof(line_buff), fd);
|
|||
|
}
|
|||
|
|
|||
|
fgets(line_buff, sizeof(line_buff), fd);
|
|||
|
sscanf(line_buff, "%s %d", name, &vmsize);
|
|||
|
fclose(fd);
|
|||
|
return vmsize;
|
|||
|
}
|
|||
|
|
|||
|
unsigned int u_get_proc_mem(unsigned int pid)
|
|||
|
{
|
|||
|
char file_name[64] = {0};
|
|||
|
FILE *fd;
|
|||
|
char line_buff[512] = {0};
|
|||
|
sprintf(file_name, "/proc/%d/status", pid);
|
|||
|
|
|||
|
fd = fopen(file_name, "r");
|
|||
|
if (nullptr == fd)
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
char name[64];
|
|||
|
int vmrss;
|
|||
|
for (int i = 0; i < VMRSS_LINE - 1; i++)
|
|||
|
{
|
|||
|
fgets(line_buff, sizeof(line_buff), fd);
|
|||
|
}
|
|||
|
|
|||
|
fgets(line_buff, sizeof(line_buff), fd);
|
|||
|
sscanf(line_buff, "%s %d", name, &vmrss);
|
|||
|
fclose(fd);
|
|||
|
|
|||
|
return vmrss;
|
|||
|
}
|
|||
|
|
|||
|
int u_get_total_mem()
|
|||
|
{
|
|||
|
const char *file = "/proc/meminfo"; //文件名
|
|||
|
FILE *fd; //定义文件指针fd
|
|||
|
char line_buff[256] = {0}; //读取行的缓冲区
|
|||
|
fd = fopen(file, "r"); //以R读的方式打开文件再赋给指针fd
|
|||
|
|
|||
|
//获取memtotal:总内存占用大小
|
|||
|
int i;
|
|||
|
char name[32]; //存放项目名称
|
|||
|
int memtotal; //存放内存峰值大小
|
|||
|
char *ret = fgets(line_buff, sizeof(line_buff), fd); //读取memtotal这一行的数据,memtotal在第1行
|
|||
|
sscanf(line_buff, "%s %d", name, &memtotal);
|
|||
|
//fprintf(stderr, "====%s:%d====\n", name, memtotal);
|
|||
|
fclose(fd); //关闭文件fd
|
|||
|
return memtotal;
|
|||
|
}
|
|||
|
|
|||
|
std::string u_getmeminfo()
|
|||
|
{
|
|||
|
pid_t pid = getpid();
|
|||
|
unsigned int virmem = u_get_proc_virtualmem(pid);
|
|||
|
unsigned int procmem = u_get_proc_mem(pid);
|
|||
|
int totalmem = u_get_total_mem();
|
|||
|
std::string strinfo = "Scanservice current memory info,virtualmem= " + std::to_string(virmem) + " KB procmem = " + std::to_string(procmem) + " KB of " + std::to_string(totalmem) + " KB total";
|
|||
|
return strinfo;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
int u_get_fileSystem_info(const char *fileSystem_name,struct _fileSystem_info *fi)
|
|||
|
{
|
|||
|
struct statfs buf;
|
|||
|
float fileSystem_total_size = 0;
|
|||
|
float fileSystem_free_size = 0;
|
|||
|
|
|||
|
if (statfs(fileSystem_name, &buf))
|
|||
|
{
|
|||
|
fprintf(stderr, "statfs %s\n", strerror(errno));
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
switch (buf.f_type)
|
|||
|
{
|
|||
|
case 0xEF51:
|
|||
|
case 0xEF53:
|
|||
|
sprintf(fi->fileSystem_format, "EXT");
|
|||
|
break;
|
|||
|
|
|||
|
case 0x4d44:
|
|||
|
sprintf(fi->fileSystem_format, "FAT");
|
|||
|
break;
|
|||
|
|
|||
|
case 0x5346544e:
|
|||
|
sprintf(fi->fileSystem_format, "NIFS");
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
sprintf(fi->fileSystem_format, "unknown");
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
// bzero(&fi->fileSystem_total_capacity,sizeof(fi->fileSystem_total_capacity));
|
|||
|
// bzero(&fi->fileSystem_free_capacity,sizeof(fi->fileSystem_free_capacity));
|
|||
|
|
|||
|
printf("blocks %ld\n", buf.f_blocks);
|
|||
|
printf("bfree %ld\n", buf.f_bfree);
|
|||
|
printf("bsize %ld\n", buf.f_bsize);
|
|||
|
|
|||
|
fileSystem_total_size =
|
|||
|
(float)(u_kscale(buf.f_blocks, buf.f_bsize));
|
|||
|
fileSystem_free_size =
|
|||
|
(float)(u_kscale(buf.f_bfree, buf.f_bsize));
|
|||
|
|
|||
|
printf("total %f\n", fileSystem_total_size);
|
|||
|
printf("free %f\n", fileSystem_free_size);
|
|||
|
|
|||
|
fi->fileSystem_total_capacity = fileSystem_total_size;
|
|||
|
fi->fileSystem_free_capacity = fileSystem_free_size;
|
|||
|
// convert_size(fileSystem_total_size,(int*)(&fi->fileSystem_total_capacity));
|
|||
|
// convert_size(fileSystem_free_size,(int*)(&fi->fileSystem_free_capacity));
|
|||
|
|
|||
|
bzero(fi->fileSystem_permissions, sizeof(fi->fileSystem_permissions));
|
|||
|
sprintf(fi->fileSystem_permissions, "rw");
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|