优化获取磁盘使用率

This commit is contained in:
aixiao 2024-05-22 15:10:19 +08:00
parent 00567557ba
commit f7f7d7136a
7 changed files with 78 additions and 32 deletions

View File

@ -6,6 +6,7 @@
"ccronexpr.h": "c", "ccronexpr.h": "c",
"clamscan.h": "c", "clamscan.h": "c",
"libiptc.h": "c", "libiptc.h": "c",
"stdio.h": "c" "stdio.h": "c",
"nginx.h": "c"
} }
} }

View File

@ -38,7 +38,7 @@ LIBCOMMON_LIB += ./clamav/common/cert_util.c.o ./clamav/common/actions.c.o ./cla
all: libclamav_rust libclamav rhost nginx.o all: libclamav_rust libclamav rhost nginx.o
rhost: conf.o rhost.o libiptc.o ccronexpr.o nginx.o rhost: conf.o rhost.o libiptc.o ccronexpr.o nginx.o disk.o
$(CC) $(ip2region_CFLAGS) ip2region/ip2region.c $(CC) $(ip2region_CFLAGS) ip2region/ip2region.c
$(CC) $(ip2region_CFLAGS) ip2region/xdb_searcher.c $(CC) $(ip2region_CFLAGS) ip2region/xdb_searcher.c
$(CC) $(cJSON_CFLAGS) cJSON/cJSON.c $(CC) $(cJSON_CFLAGS) cJSON/cJSON.c

59
disk.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mntent.h>
#include <sys/statvfs.h>
// 获取指定路径的磁盘使用率
int get_disk_usage(const char *path, double *usage) {
struct statvfs stat;
if (statvfs(path, &stat) != 0) {
// 处理错误
perror("statvfs failed");
return -1;
}
// 计算磁盘使用率
unsigned long total_blocks = stat.f_blocks;
unsigned long free_blocks = stat.f_bfree;
unsigned long used_blocks = total_blocks - free_blocks;
*usage = (double)used_blocks / total_blocks * 100;
return 0;
}
int disk_usage() {
FILE *mounts;
struct mntent *ent;
mounts = setmntent("/etc/mtab", "r");
if (mounts == NULL) {
perror("setmntent failed");
return 1;
}
while ((ent = getmntent(mounts)) != NULL)
{
double usage = 0;
if (strstr(ent->mnt_fsname, "/dev/") != NULL)
{
//printf("%s %s %s\n", ent->mnt_fsname, ent->mnt_dir, ent->mnt_type);
if (get_disk_usage(ent->mnt_dir, &usage) != 0) {
fprintf(stderr, "Failed to get disk usage for %s\n", ent->mnt_dir);
continue;
}
int threshold = 1;
if (usage > threshold) {
printf("挂载点: %s 使用率: %.2f%% 阀值: %d%%\n", ent->mnt_dir, usage, threshold);
}
}
}
endmntent(mounts);
return 0;
}

12
disk.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef DISK_H
#define DISK_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mntent.h>
#include <sys/statvfs.h>
extern int disk_usage();
#endif

View File

@ -1,4 +1,3 @@
#include "nginx.h" #include "nginx.h"
#define EVENT_SIZE (sizeof(struct inotify_event)) #define EVENT_SIZE (sizeof(struct inotify_event))

31
rhost.c
View File

@ -458,30 +458,6 @@ int isregion(char *str, char (*region_list)[WHITELIST_IP_NUM])
return 0; return 0;
} }
// 去除空格
char *remove_space(char *str)
{
unsigned int i = 0, j = 0;
unsigned int uLen = _strlen(str);
char *strRet;
if (0 == uLen) {
return '\0';
}
strRet = (char *)malloc(uLen + 2);
memset(strRet, 0, uLen + 2);
for (i = 0; i < uLen; i++) {
if (str[i] != ' ') {
strRet[j++] = str[i];
}
}
strRet[j] = '\0';
return strRet;
}
// 磁盘使用率 // 磁盘使用率
int disk_waring(int threshold) int disk_waring(int threshold)
{ {
@ -756,7 +732,6 @@ BLOCKED:
if (fp != NULL) if (fp != NULL)
pclose(fp); pclose(fp);
if (fc != NULL) if (fc != NULL)
pclose(fc); pclose(fc);
if (t) if (t)
@ -1027,7 +1002,6 @@ int main(int argc, char *argv[], char **env)
int head_argc = 0; int head_argc = 0;
char *argvs[ARGS_NUM] = { NULL }; char *argvs[ARGS_NUM] = { NULL };
char args[ARGS_NUM][WHITELIST_IP_NUM] = { { 0 }, { 0 } }; char args[ARGS_NUM][WHITELIST_IP_NUM] = { { 0 }, { 0 } };
//printf("%d\n", argc);
if (argc > 3) // 手动输入参数(如果手动输入参数个数大于3个, 则使用用户输入的参数) if (argc > 3) // 手动输入参数(如果手动输入参数个数大于3个, 则使用用户输入的参数)
{ {
process_argv(argc, argv, &(argvs[0])); process_argv(argc, argv, &(argvs[0]));
@ -1196,8 +1170,8 @@ goto_daemon:
printf("Disk usage does not reach threshold!\n"); printf("Disk usage does not reach threshold!\n");
} }
} }
_exit(r); _exit(r);
} else { } else {
int status = 0; int status = 0;
wait(&status); // wait the end of child process wait(&status); // wait the end of child process
@ -1210,9 +1184,10 @@ goto_daemon:
} }
} }
} }
// 封禁非法IP
rule(conf); rule(conf);
sleep(conf->TIME); sleep(conf->TIME);
} }
} else { } else {
; ;

View File

@ -17,7 +17,7 @@ global {
CLAMAV = 1; // clamav 是否扫描病毒(1开启,非1关闭) CLAMAV = 1; // clamav 是否扫描病毒(1开启,非1关闭)
CLAMAV_ARG = "-r / --exclude-dir=^/sys|^/dev|^/proc|^/opt/infected|^/root|^/home|^/mnt|^/usr|^/var --move=/opt/infected --max-filesize 1024M -l clamscan.log"; CLAMAV_ARG = "-r / --exclude-dir=^/sys|^/dev|^/proc|^/opt/infected|^/root|^/home|^/mnt|^/usr|^/var --move=/opt/infected --max-filesize 1024M -l clamscan.log";
CLAMAV_TIME = "* 7 23 * * *"; // clamav 扫描时间(Cron格式, 秒 分 时 天 月 周) CLAMAV_TIME = "* 45 11 * * *"; // clamav 扫描时间(Cron格式, 秒 分 时 天 月 周)
IPV4_RESTRICTION = 1; // 是否启用IP白名单(1开启,非1关闭) IPV4_RESTRICTION = 1; // 是否启用IP白名单(1开启,非1关闭)