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