Imported Upstream version 2.7
[ossec-hids.git] / src / util / clear_stats.c
1 /* @(#) $Id: ./src/util/clear_stats.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
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
10  * Foundation
11  */
12
13
14 /* This tool will clear the project statistics */
15
16 #include "shared.h"
17
18 #undef ARGV0
19 #define ARGV0 "clear_stats"
20
21
22 /** help **/
23 void helpmsg()
24 {
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");
31     exit(1);
32 }
33
34
35 /** main **/
36 int main(int argc, char **argv)
37 {
38     int clear_daily = 0;
39     int clear_weekly = 0;
40
41     char *dir = DEFAULTDIR;
42     char *group = GROUPGLOBAL;
43     char *user = USER;
44     int gid;
45     int uid;
46
47
48     /* Setting the name */
49     OS_SetName(ARGV0);
50
51
52     /* user arguments */
53     if(argc != 2)
54     {
55         helpmsg();
56     }
57
58     /* Getting the group name */
59     gid = Privsep_GetGroup(group);
60     uid = Privsep_GetUser(user);
61     if(gid < 0)
62     {
63             ErrorExit(USER_ERROR, ARGV0, user, group);
64     }
65         
66
67     /* Setting the group */
68     if(Privsep_SetGroup(gid) < 0)
69     {
70             ErrorExit(SETGID_ERROR,ARGV0, group);
71     }
72
73
74     /* Chrooting to the default directory */
75     if(Privsep_Chroot(dir) < 0)
76     {
77         ErrorExit(CHROOT_ERROR, ARGV0, dir);
78     }
79
80
81     /* Inside chroot now */
82     nowChroot();
83
84
85     /* Setting the user */
86     if(Privsep_SetUser(uid) < 0)
87     {
88         ErrorExit(SETUID_ERROR, ARGV0, user);
89     }
90
91     /* User options */
92     if(strcmp(argv[1], "-h") == 0)
93     {
94         helpmsg();
95     }
96     else if(strcmp(argv[1], "-a") == 0)
97     {
98         clear_daily = 1;
99         clear_weekly = 1;
100     }
101     else if(strcmp(argv[1], "-d") == 0)
102     {
103         clear_daily = 1;
104     }
105     else if(strcmp(argv[1], "-w") == 0)
106     {
107         clear_weekly = 1;
108     }
109     else
110     {
111         printf("\n** Invalid option '%s'.\n", argv[1]);
112         helpmsg();
113     }
114
115
116     /* Clear daily files */
117     if(clear_daily)
118     {
119         char *daily_dir = STATQUEUE;
120         DIR *daily;
121         struct dirent *entry;
122
123         daily = opendir(daily_dir);
124         if(!daily)
125         {
126             ErrorExit("%s: Unable to open: '%s'", ARGV0, daily_dir);
127         }
128
129         while((entry = readdir(daily)) != NULL)
130         {
131             char full_path[OS_MAXSTR +1];
132
133             /* Do not even attempt to delete . and .. :) */
134             if((strcmp(entry->d_name,".") == 0)||
135                (strcmp(entry->d_name,"..") == 0))
136             {
137                 continue;
138             }
139
140             /* Remove file */
141             full_path[OS_MAXSTR] = '\0';
142             snprintf(full_path, OS_MAXSTR, "%s/%s", daily_dir, entry->d_name);
143             unlink(full_path);
144         }
145
146         closedir(daily);
147     }
148
149
150     /* Clear weekly averages */
151     if(clear_weekly)
152     {
153         int i = 0;
154         while(i <= 6)
155         {
156             char *daily_dir = STATWQUEUE;
157             char dir_path[OS_MAXSTR +1];
158             DIR *daily;
159             struct dirent *entry;
160
161             snprintf(dir_path, OS_MAXSTR, "%s/%d", daily_dir, i);
162             daily = opendir(dir_path);
163             if(!daily)
164             {
165                 ErrorExit("%s: Unable to open: '%s' (no stats)",
166                            ARGV0, dir_path);
167             }
168
169             while((entry = readdir(daily)) != NULL)
170             {
171                 char full_path[OS_MAXSTR +1];
172
173                 /* Do not even attempt to delete . and .. :) */
174                 if((strcmp(entry->d_name,".") == 0)||
175                         (strcmp(entry->d_name,"..") == 0))
176                 {
177                     continue;
178                 }
179
180                 /* Remove file */
181                 full_path[OS_MAXSTR] = '\0';
182                 snprintf(full_path, OS_MAXSTR, "%s/%s", dir_path,
183                                                         entry->d_name);
184                 unlink(full_path);
185             }
186
187             i++;
188             closedir(daily);
189         }
190     }
191
192     printf("\n** Internal stats clear.\n\n");
193     return(0);
194 }
195
196
197 /* EOF */