说明:采用的是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 longlong 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 longlong 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
.h
#ifndef _DATA_ACQUISITION_
#define _DATA_ACQUISITION_
#include "Peer2PeerData.h"
#include <vector>
#include <string>
usingnamespace 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, constchar* 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(constchar* szFile, constchar* 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(constchar* szProcessName, int &nPid);
/*------ Process End ------*//*------PC Start------*/public:
/**
* Ping.
*
* @param -[in] char* szHostName: [主机地址]
* @return bool.
* @version 10/28/2016 W Initial Version
**/bool Ping(constchar *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
#include "DataAcquisition.h"//#include <cstdio> #include <stdio.h> #include <sstream> #ifdef WIN32 //#define WIN32_LEAN_AND_MEAN 1#define C_DRIVERLETTER 0x43 #include <tchar.h> #include <ctype.h> #include <Winsock2.h> #include <WS2tcpip.h> #include <Windows.h> #include <TlHelp32.h> #include <Pdh.h> #pragma comment(lib, "Pdh.lib") #include <Psapi.h> #pragma comment(lib, "Psapi.lib") #include <IPHlpApi.h> #pragma comment(lib, "Iphlpapi.lib") #include <WinIoCtl.h> #include <ShellAPI.h> #include <devguid.h> #include <Setupapi.h> #pragma comment(lib, "Setupapi.lib") PDH_HQUERY g_Query = NULL; PDH_HCOUNTER *g_pCounters = NULL; PDH_FMT_COUNTERVALUE *g_pCounterValue = NULL; #else #ifdef __GNUC__ #include "x86intrin.h"#define __rdtsc() __builtin_ia32_rdtsc() #endif #include <iostream> #include <vector> #include <string> #include <stdio.h> #include <cstring> #include <sys/times.h> #include <sys/vtimes.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/sysinfo.h> #include <stdlib.h> #include <dirent.h> #include <fstream> #include <unistd.h> #include <netdb.h> #include <arpa/inet.h> #include <map> #define HERTZ (sysconf(_SC_CLK_TCK) * 1.0) #define READ_CHARS_LENGTH 256 #endifusingnamespace std; DataAcquisition::DataAcquisition(void) : m_nProcessorCores(0), m_nPhysicalMemory(0), m_nVirtualMemory(0) { } DataAcquisition::~DataAcquisition(void) { #ifdef WIN32 if(g_Query) PdhCloseQuery(g_Query); if(g_pCounters != NULL || g_pCounterValue != NULL) { delete[] g_pCounters; delete[] g_pCounterValue; } #else#endif } /*------Global Function Start------*/ #ifdef WIN32 bool AdjustTokenPrivilege(HANDLE hProcess, DWORD nDesiredAccess = TOKEN_ALL_ACCESS /* TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY */) { HANDLE hToken = NULL; if(!OpenProcessToken(hProcess, nDesiredAccess, &hToken)) { if(hToken != NULL) CloseHandle(hToken); if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) returntrue; elsereturnfalse; } TOKEN_PRIVILEGES tokenPrivileges; if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME/*SE_SECURITY_NAME*/, &tokenPrivileges.Privileges[0].Luid)) { if(hToken != NULL) CloseHandle(hToken); returnfalse; } tokenPrivileges.PrivilegeCount = 1; tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; BOOL bReturn = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL); if(hToken != NULL) CloseHandle(hToken); return bReturn == TRUE; } int TheProcessIDByWindowName(constchar* szWinName) { DWORD ProcessID = -1; HWND hWnd = FindWindow(NULL, szWinName); if (hWnd != NULL) GetWindowThreadProcessId(hWnd, &ProcessID); return ProcessID; } HANDLE TheProcessByID(int nPid, unsigned long nDesiredAccess = PROCESS_QUERY_INFORMATION /* PROCESS_ALL_ACCESS */) { return OpenProcess(nDesiredAccess, FALSE, nPid); } #elsevoid LinesFrom(FILE *pFile, vector<string> &vResult) { vResult.clear(); char szLine[READ_CHARS_LENGTH] = { 0 }; while(fgets(szLine, READ_CHARS_LENGTH, pFile) != NULL) { int nLength = strlen(szLine); if(szLine[nLength - 1] == ‘n‘) szLine[nLength - 1] = ‘