1 /* @(#) $Id: ./src/util/clear_stats.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
14 /* This tool will clear the project statistics */
19 #define ARGV0 "clear_stats"
25 printf("\nOSSEC HIDS %s: Clear the events stats (averages).\n", ARGV0);
26 printf("Available options:\n");
27 printf("\t-h This help message.\n");
28 printf("\t-a Clear all the stats (averages).\n");
29 printf("\t-d Clear the daily averages.\n");
30 printf("\t-w Clear the weekly averages.\n\n");
36 int main(int argc, char **argv)
41 char *dir = DEFAULTDIR;
42 char *group = GROUPGLOBAL;
48 /* Setting the name */
58 /* Getting the group name */
59 gid = Privsep_GetGroup(group);
60 uid = Privsep_GetUser(user);
63 ErrorExit(USER_ERROR, ARGV0, user, group);
67 /* Setting the group */
68 if(Privsep_SetGroup(gid) < 0)
70 ErrorExit(SETGID_ERROR,ARGV0, group);
74 /* Chrooting to the default directory */
75 if(Privsep_Chroot(dir) < 0)
77 ErrorExit(CHROOT_ERROR, ARGV0, dir);
81 /* Inside chroot now */
85 /* Setting the user */
86 if(Privsep_SetUser(uid) < 0)
88 ErrorExit(SETUID_ERROR, ARGV0, user);
92 if(strcmp(argv[1], "-h") == 0)
96 else if(strcmp(argv[1], "-a") == 0)
101 else if(strcmp(argv[1], "-d") == 0)
105 else if(strcmp(argv[1], "-w") == 0)
111 printf("\n** Invalid option '%s'.\n", argv[1]);
116 /* Clear daily files */
119 char *daily_dir = STATQUEUE;
121 struct dirent *entry;
123 daily = opendir(daily_dir);
126 ErrorExit("%s: Unable to open: '%s'", ARGV0, daily_dir);
129 while((entry = readdir(daily)) != NULL)
131 char full_path[OS_MAXSTR +1];
133 /* Do not even attempt to delete . and .. :) */
134 if((strcmp(entry->d_name,".") == 0)||
135 (strcmp(entry->d_name,"..") == 0))
141 full_path[OS_MAXSTR] = '\0';
142 snprintf(full_path, OS_MAXSTR, "%s/%s", daily_dir, entry->d_name);
150 /* Clear weekly averages */
156 char *daily_dir = STATWQUEUE;
157 char dir_path[OS_MAXSTR +1];
159 struct dirent *entry;
161 snprintf(dir_path, OS_MAXSTR, "%s/%d", daily_dir, i);
162 daily = opendir(dir_path);
165 ErrorExit("%s: Unable to open: '%s' (no stats)",
169 while((entry = readdir(daily)) != NULL)
171 char full_path[OS_MAXSTR +1];
173 /* Do not even attempt to delete . and .. :) */
174 if((strcmp(entry->d_name,".") == 0)||
175 (strcmp(entry->d_name,"..") == 0))
181 full_path[OS_MAXSTR] = '\0';
182 snprintf(full_path, OS_MAXSTR, "%s/%s", dir_path,
192 printf("\n** Internal stats clear.\n\n");