diff --git a/.vscode/settings.json b/.vscode/settings.json index 5455103..c968ffd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "ccronexpr.h": "c", "clamscan.h": "c", "libiptc.h": "c", - "stdio.h": "c" + "stdio.h": "c", + "nginx.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index bdf67b6..008976e 100644 --- a/Makefile +++ b/Makefile @@ -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 -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/xdb_searcher.c $(CC) $(cJSON_CFLAGS) cJSON/cJSON.c diff --git a/disk.c b/disk.c new file mode 100644 index 0000000..50da416 --- /dev/null +++ b/disk.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include + +// 获取指定路径的磁盘使用率 +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; +} diff --git a/disk.h b/disk.h new file mode 100644 index 0000000..7593432 --- /dev/null +++ b/disk.h @@ -0,0 +1,12 @@ +#ifndef DISK_H +#define DISK_H + +#include +#include +#include +#include +#include + +extern int disk_usage(); + +#endif diff --git a/nginx.c b/nginx.c index eb687b8..f8dc501 100644 --- a/nginx.c +++ b/nginx.c @@ -1,4 +1,3 @@ - #include "nginx.h" #define EVENT_SIZE (sizeof(struct inotify_event)) diff --git a/rhost.c b/rhost.c index 28de681..9402b9e 100644 --- a/rhost.c +++ b/rhost.c @@ -458,30 +458,6 @@ int isregion(char *str, char (*region_list)[WHITELIST_IP_NUM]) 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) { @@ -756,7 +732,6 @@ BLOCKED: if (fp != NULL) pclose(fp); - if (fc != NULL) pclose(fc); if (t) @@ -1027,7 +1002,6 @@ int main(int argc, char *argv[], char **env) int head_argc = 0; char *argvs[ARGS_NUM] = { NULL }; char args[ARGS_NUM][WHITELIST_IP_NUM] = { { 0 }, { 0 } }; - //printf("%d\n", argc); if (argc > 3) // 手动输入参数(如果手动输入参数个数大于3个, 则使用用户输入的参数) { process_argv(argc, argv, &(argvs[0])); @@ -1196,8 +1170,8 @@ goto_daemon: printf("Disk usage does not reach threshold!\n"); } } - _exit(r); + } else { int status = 0; wait(&status); // wait the end of child process @@ -1210,9 +1184,10 @@ goto_daemon: } } } - + // 封禁非法IP rule(conf); sleep(conf->TIME); + } } else { ; diff --git a/rhost.conf b/rhost.conf index 25d8af3..7afeacf 100644 --- a/rhost.conf +++ b/rhost.conf @@ -17,7 +17,7 @@ global { 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_TIME = "* 7 23 * * *"; // clamav 扫描时间(Cron格式, 秒 分 时 天 月 周) + CLAMAV_TIME = "* 45 11 * * *"; // clamav 扫描时间(Cron格式, 秒 分 时 天 月 周) IPV4_RESTRICTION = 1; // 是否启用IP白名单(1开启,非1关闭)