在Linux系统中,有多种方法可以实现监控。这里,我将向您展示如何使用C++和一些常用的Linux库来实现系统监控。
sys/sysinfo.h
库获取系统信息:#include <iostream>
#include <sys/sysinfo.h>
int main() {
struct sysinfo info;
if (sysinfo(&info) != 0) {
std::cerr << "获取系统信息失败" << std::endl;
return 1;
}
std::cout << "系统内存大小: " << info.totalram / (1024 * 1024) << "MB" << std::endl;
std::cout << "可用内存大小: " << info.freeram / (1024 * 1024) << "MB" << std::endl;
std::cout << "系统负载: " << info.loads[0] / (double)sysconf(_SC_NPROCESSORS_ONLN) << std::endl;
return 0;
}
/proc/stat
文件获取CPU和内存使用情况:#include <iostream>
#include <fstream>
#include <string>
void read_cpu_info(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "打开文件失败" << std::endl;
return;
}
std::string line;
long long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
while (getline(file, line)) {
sscanf(line.c_str(), "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld",
&user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
std::cout << "用户态CPU使用率: " << user / (double)sysconf(_SC_CLK_TCK) << "%" << std::endl;
std::cout << "系统态CPU使用率: " << system / (double)sysconf(_SC_CLK_TCK) << "%" << std::endl;
std::cout << "空闲CPU时间: " << idle / (double)sysconf(_SC_CLK_TCK) << "%" << std::endl;
}
}
void read_mem_info(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "打开文件失败" << std::endl;
return;
}
std::string line;
long long memtotal, memfree, buffers, cached, shmmem;
while (getline(file, line)) {
sscanf(line.c_str(), "%lld %lld %lld %lld %lld", &memtotal, &memfree, &buffers, &cached, &shmmem);
std::cout << "总内存大小: " << memtotal / (1024 * 1024) << "MB" << std::endl;
std::cout << "可用内存大小: " << memfree / (1024 * 1024) << "MB" << std::endl;
}
}
int main() {
read_cpu_info("/proc/stat");
read_mem_info("/proc/meminfo");
return 0;
}
inotify-tools
库监控文件系统事件:首先,您需要安装inotify-tools
库。在Debian和Ubuntu系统中,可以使用以下命令安装:
sudo apt-get install inotify-tools
在CentOS和RHEL系统中,可以使用以下命令安装:
sudo yum install inotify-tools
接下来,您可以使用以下C++代码监控文件系统事件:
#include <iostream>
#include <inotify.h>
#include <sys/inotify.h>
#include <unistd.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main() {
int length, i = 0;
int fd = inotify_init();
if (fd < 0) {
std::cerr << "inotify_init失败" << std::endl;
return 1;
}
int wd = inotify_add_watch(fd, "/path/to/monitor", IN_MODIFY | IN_CREATE | IN_DELETE);
if (wd < 0) {
std::cerr << "inotify_add_watch失败" << std::endl;
close(fd);
return 1;
}
char buffer[BUF_LEN];
while (1) {
length = read(fd, buffer, BUF_LEN);
if (length < 0) {
std::cerr << "read失败" << std::endl;
break;
}
while (i < length) {
struct inotify_event *event = (struct inotify_event *)&buffer[i];
if (event->len) {
if (event->mask & IN_CREATE) {
std::cout << "文件或目录已创建" << std::endl;
} else if (event->mask & IN_DELETE) {
std::cout << "文件或目录已被删除" << std::endl;
} else if (event->mask & IN_MODIFY) {
std::cout << "文件或目录已修改" << std::endl;
}
}
i += EVENT_SIZE + event->len;
}
i = 0;
}
inotify_rm_watch(fd, wd);
close(fd);
return 0;
}
请注意,您需要将/path/to/monitor
替换为您要监控的文件或目录的路径。
这些方法可以帮助您实现Linux系统的监控。您可以根据自己的需求选择合适的方法,并根据需要进行调整。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。