一个Window/Linux(Fedora测试平台)的CPU,磁盘,内存,PC,进程相关信息采集功能

时间:2021-10-06 22:50:19

说明:采用的是Multi-Byte Character Set,,不支持Unicode。

Peer2PeerData.h

#ifndef _PEER_2_PEER_DATA_H #define _PEER_2_PEER_DATA_H #include <cstring> /* unsigned long long */ typedef unsigned long long ULLong; #define ELAPSEDSECONDS 10000000 #define ADAYTOTALSECONDS (1 * 24 * 60 * 60) #define RECEIVED_BUFFER_LENGTH (1024 * 4) #define QUERY_SLEEP_TIMESTAMP_MS 1024 #define MESSAGEMAXLENGGTH (1024 * 1024 * 128) #define HEADER 0x02 #define PACKHEADERLENGTH (1 + sizeof(int)) #define TYPE_CPUUSAGE 1 #define TYPE_MEMORYUSAGE 2 #define TYPE_DRIVEUSAGE 3 #define TYPE_RUNTHEFILE 4 #define TYPE_PROCESSINFO 5 #define TYPE_CMD_CURRENT 20 #define TYPE_CMD_AVERAGE 702 #define TYPE_CMD_APPOINT 888 #define TYPE_CMD_RESERVE 999 #define CMD_CALC_UNIT_MINUTES 101 #define CMD_CALC_UNIT_HOURS 45 #define UNIT_VALUE_ONE 1 #define UNIT_VALUE_THREE 3 #define UNIT_VALUE_SIX 6 #define UNIT_VALUE_TEN 10 #define UNIT_VALUE_TWELVE 12 #define UNIT_VALUE_TWENTYFOUR 24 #define UNIT_VALUE_THIRTY 30 #define UNIT_VALUE_SIXTY 60 /* 磁盘/挂载/盘符容量信息 */ struct CapacityInfo { /* 总值(kB) */ ULLong m_nTotal; /* 空闲值(kB) */ ULLong m_nFree; /* 磁盘/挂载/盘符名称 */ char m_szName[128]; CapacityInfo() : m_nTotal(0), m_nFree(0) { memset(m_szName, 0, sizeof(m_szName)); } }; /* 进程信息 */ struct ProcessInfo { /* Start-Time Year */ unsigned short m_nStartTime_Year; /* Start-Time Month */ unsigned short m_nStartTime_Month; /* Start-Time Day */ unsigned short m_nStartTime_Day; /* Start-Time Hour */ unsigned short m_nStartTime_Hour; /* Start-Time Minute */ unsigned short m_nStartTime_Minute; /* Start-Time Second */ unsigned short m_nStartTime_Second; /* Pid */ unsigned short m_nPid; /* Run-Time(s) */ unsigned long long m_nRunTime; /* Process Name */ char m_szName[128]; /* Process File-Name */ char m_szFileName[256]; #ifdef WIN32 /* Process Describe */ char m_szDescribe[512]; #endif ProcessInfo() : m_nPid(0), m_nRunTime(0) { m_nStartTime_Year = m_nStartTime_Month = m_nStartTime_Day = m_nStartTime_Hour = m_nStartTime_Minute = m_nStartTime_Second = 0; memset(m_szName, 0, sizeof(m_szName)); memset(m_szFileName, 0, sizeof(m_szFileName)); #ifdef WIN32 memset(m_szDescribe, 0, sizeof(m_szDescribe)); #endif } }; #endif //_PEER_2_PEER_DATA_H

View Code

.h

#ifndef _DATA_ACQUISITION_ #define _DATA_ACQUISITION_ #include "Peer2PeerData.h" #include <vector> #include <string> using namespace std; /* Data Acquisition (DAQ) */ class DataAcquisition { public: DataAcquisition(void); ~DataAcquisition(void); /*------CPU Start------*/ public: /** * CPU核数. * * @return int. * @version 10/28/2016 W Initial Version **/ int ProcessorCores(); /** * CPU各核的使用率(%). * * @param -[in,out] vector<double> &vUtilization: [使用率] * @return int. * @version 10/28/2016 W Initial Version * @example cpu : 2.47525(%) * cpu0: 1.96078(%) * cpu1: 2.2703(%) * ... **/ int CpuUsageOfCores(vector<double> &vUtilization); /** * 进程的CPU使用率(%). * * @param -[in,out] double &nUtilize: [使用率] * @param -[in] int nPid: [PID] * @return int. * @version 10/28/2016 W Initial Version * @remark -[Pid = -1] Mean Current Process * @example 2.47525(%) **/ int CpuUsageByProcess(double &nUtilize, int nPid = -1); private: /* CPU Cores */ int m_nProcessorCores; /*------ CPU End ------*/ /*------Memory Start------*/ public: /** * 物理内存(kB). * * @param -[in,out] ULLong &nTotal: [总值] * @return int. * @version 10/28/2016 W Initial Version * @example 1024(kB) **/ int PhysicalMemory(ULLong &nTotal); /** * 空闲物理内存(kB). * * @param -[in,out] ULLong &nFree: [空闲值] * @return int. * @version 10/28/2016 W Initial Version * @example 512(kB) **/ int PhysicalMemoryFree(ULLong &nFree); /** * 进程的物理内存使用值(kB). * * @param -[in,out] ULLong &nUsage: [使用值] * @param -[in] int nPid: [PID] * @return int. * @version 10/28/2016 W Initial Version * @remark -[Pid = -1] Mean Current Process * @example 64(kB) **/ int PhysicalMemroyUsageByProcess(ULLong &nUsage, int nPid = -1); /** * 虚拟内存(kB). * * @param -[in,out] ULLong &nTotal: [总值] * @return int. * @version 10/28/2016 W Initial Version * @example 1024(kB) **/ int VirtualMemory(ULLong &nTotal); /** * 空闲虚拟内存(kB). * * @param -[in,out] ULLong &nFree: [空闲值] * @return int. * @version 10/28/2016 W Initial Version * @example 512(kB) **/ int VirtualMemoryFree(ULLong &nFree); /** * 进程的虚拟内存使用值(kB). * * @param -[in,out] ULLong &nUsage: [使用值] * @param -[in] int nPid: [PID] * @return int. * @version 10/28/2016 W Initial Version * @remark -[Pid = -1] Mean Current Process * @example 64(kB) **/ int VirtualMemroyUsageByProcess(ULLong &nUsage, int nPid = -1); private: /* 物理内存(kB) */ ULLong m_nPhysicalMemory; /* 虚拟内存(kB) */ ULLong m_nVirtualMemory; /*------ Memory End ------*/ /*------Disk Start------*/ public: /** * 磁盘数. * * @return int. * @version 10/28/2016 W Initial Version **/ int DiskCount(); /** * 挂载/盘符数. * * @return int. * @version 10/28/2016 W Initial Version **/ int DriveLetterCount(); /** * 磁盘容量信息. * * @param -[in,out] vector<CapacityInfo> &vCapacityInfo: [容量信息] * @return int. * @version 10/28/2016 W Initial Version **/ int TheDiskCapacityInfo(vector<CapacityInfo*> &vCapacityInfo); /** * 挂载/盘符容量信息. * * @param -[in,out] vector<CapacityInfo> &vCapacityInfo: [容量信息] * @param -[in] const char* szDriveLetter: [挂载/盘符] * @return int. * @version 10/28/2016 W Initial Version * @remark -[szDriveLetter = NULL] Mean All Drive-Letter * @example Windows(szDriveLetter = ‘C:\‘), Linux(szDriveLetter = ‘/root‘) **/ int TheDriveLetterCapacityInfo(vector<CapacityInfo*> &vCapacityInfo, const char* szDriveLetter = NULL); /*------ Disk End ------*/ /*------Process Start------*/ public: /** * 打开文件. * * @param -[in] const char* szFile: [文件] * @param -[in] const char* szParameters: [参数] * @return int. * @version 10/28/2016 W Initial Version * @remark -[szParameters = NULL] Mean Without Parameter **/ int OpenTheFile(const char* szFile, const char* szParameters = NULL); /** * 进程信息. * * @param -[in,out] vector<ProcessInfo*> &vProcessInfo: [进程信息] * @param -[in] int nPid: [PID] * @return int. * @version 10/28/2016 W Initial Version * @remark -[nPid = -1] Mean All Process-Info **/ int Processes(vector<ProcessInfo*> &vProcessInfo, int nPid = -1); /** * 进程信息. * * @param -[in] const char* szProcessName: [进程名称] * @param -[in,out] int &nPid: [PID] * @return int. * @version 10/28/2016 W Initial Version * @remark -[nPid = -1] Mean Error Occurred **/ int TheProcessIDByName(const char* szProcessName, int &nPid); /*------ Process End ------*/ /*------PC Start------*/ public: /** * Ping. * * @param -[in] char* szHostName: [主机地址] * @return bool. * @version 10/28/2016 W Initial Version **/ bool Ping(const char *szIPAddress); /** * 主机名称. * * @param -[in] char* szHostName: [主机名称] * @param -[in,out] unsigned long &nLength: [长度] * @return int. * @version 10/28/2016 W Initial Version * @remark -[nPid = -1] Mean Error Occurred **/ int TheHostName(char* szHostName, unsigned long &nLength); /** * 当前用户名称. * * @param -[in] char* szUserName: [用户名称] * @param -[in,out] unsigned long &nLength: [长度] * @return int. * @version 10/28/2016 W Initial Version **/ int TheUserName(char* szUserName, unsigned long &nLength); /** * 主机IP. * * @param -[in,out] vector<string> &vIPAddress: [IP] * @param -[in] unsigned short nFamily: [协议] * @return int. * @version 10/28/2016 W Initial Version * @remark OS Family Value * Windows AF_INET 2 * Windows/Linux AF_INET6 23/10 * Windows AF_UNSPEC 0 **/ int TheHostIPAddress(vector<string> &vIPAddress, unsigned short nFamily /* = AF_INET or AF_INET6 or AF_UNSPEC*/); /*------ PC End ------*/ /*------Net Start------*/ /*------ Net End ------*/ }; #endif //_DATA_ACQUISITION_

.cpp