1 /* @(#) $Id: clear_stats.c,v 1.6 2009/06/24 18:53:09 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
13 /* This tool will clear the project statistics */
18 #define ARGV0 "clear_stats"
24 printf("\nOSSEC HIDS %s: Clear the events stats (averages).\n", ARGV0);
25 printf("Available options:\n");
26 printf("\t-h This help message.\n");
27 printf("\t-a Clear all the stats (averages).\n");
28 printf("\t-d Clear the daily averages.\n");
29 printf("\t-w Clear the weekly averages.\n\n");
35 int main(int argc, char **argv)
40 char *dir = DEFAULTDIR;
41 char *group = GROUPGLOBAL;
47 /* Setting the name */
57 /* Getting the group name */
58 gid = Privsep_GetGroup(group);
59 uid = Privsep_GetUser(user);
62 ErrorExit(USER_ERROR, ARGV0, user, group);
66 /* Setting the group */
67 if(Privsep_SetGroup(gid) < 0)
69 ErrorExit(SETGID_ERROR,ARGV0, group);
73 /* Chrooting to the default directory */
74 if(Privsep_Chroot(dir) < 0)
76 ErrorExit(CHROOT_ERROR, ARGV0, dir);
80 /* Inside chroot now */
84 /* Setting the user */
85 if(Privsep_SetUser(uid) < 0)
87 ErrorExit(SETUID_ERROR, ARGV0, user);
91 if(strcmp(argv[1], "-h") == 0)
95 else if(strcmp(argv[1], "-a") == 0)
100 else if(strcmp(argv[1], "-d") == 0)
104 else if(strcmp(argv[1], "-w") == 0)
110 printf("\n** Invalid option '%s'.\n", argv[1]);
115 /* Clear daily files */
118 char *daily_dir = STATQUEUE;
120 struct dirent *entry;
122 daily = opendir(daily_dir);
125 ErrorExit("%s: Unable to open: '%s'", ARGV0, daily_dir);
128 while((entry = readdir(daily)) != NULL)
130 char full_path[OS_MAXSTR +1];
132 /* Do not even attempt to delete . and .. :) */
133 if((strcmp(entry->d_name,".") == 0)||
134 (strcmp(entry->d_name,"..") == 0))
140 full_path[OS_MAXSTR] = '\0';
141 snprintf(full_path, OS_MAXSTR, "%s/%s", daily_dir, entry->d_name);
149 /* Clear weekly averages */
155 char *daily_dir = STATWQUEUE;
156 char dir_path[OS_MAXSTR +1];
158 struct dirent *entry;
160 snprintf(dir_path, OS_MAXSTR, "%s/%d", daily_dir, i);
161 daily = opendir(dir_path);
164 ErrorExit("%s: Unable to open: '%s' (no stats)",
168 while((entry = readdir(daily)) != NULL)
170 char full_path[OS_MAXSTR +1];
172 /* Do not even attempt to delete . and .. :) */
173 if((strcmp(entry->d_name,".") == 0)||
174 (strcmp(entry->d_name,"..") == 0))
180 full_path[OS_MAXSTR] = '\0';
181 snprintf(full_path, OS_MAXSTR, "%s/%s", dir_path,
191 printf("\n** Internal stats clear.\n\n");