4 * Purpose: aMule Statistics
6 * Author: Pedro de Oliveira <falso@rdk.homeip.net>
8 * Copyright (C) 2004 by Pedro de Oliveira
10 * This file is part of aMule.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the
24 * Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include <sys/types.h>
37 #include "configfile.h"
38 #include "functions.h"
46 * ????.??.?? - falso: creation of cas.
47 * ????.??.?? - Jacobo221: Detect connecting state
48 * ????.??.?? - falso: HTML page generation
49 * 2004.08.27 - GonoszTopi: New line handling routines, to cope with lines
50 * longer than 80 characters. Fixes buffer overflow.
51 * 2005.12.10 - fulgas: added kad info support
52 * 2005,12,16 - stefanero: fixed Kad related stuff and some other things
55 static struct option long_options
[] = {
56 { "help", no_argument
, NULL
, 'h' },
57 { "html", optional_argument
, NULL
, 'p' },
58 { "picture", optional_argument
, NULL
, 'o' },
59 { "config-dir", required_argument
, NULL
, 'c' },
63 void usage(char *myname
)
65 printf (" ___ _ _ ___ c aMule statistics\n"
66 " /'___) /'_` )/',__) by Pedro de Oliveira\n"
67 "( (___ ( (_| |\\__, \\ <falso@rdk.homeip.net>\n"
68 "`\\____)`\\__,_)(____/ Version %s\n\n"
70 "Usage: %s [OPTION]\n"
71 "If run without any option prints stats to stdout\n\n"
74 "-o, --picture, -P\tWrites the online signature picture\n"
76 "-p, --html, -H\t\tHTML Page with stats and picture\n"
77 "-c, --config-dir\tSpecifies a config-dir different from home\n"
78 "-h, --help\t\tThis help you're reading\n", CAS_VERSION
, myname
);
81 int main(int argc
, char *argv
[])
83 /* Declaration of variables */
87 char *config_path
=NULL
;
95 char *path_for_picture
=NULL
;
96 char *path_for_html
=NULL
;
102 while ((c
= getopt_long (argc
, argv
, "c:P:H:hpo", long_options
, NULL
)) != -1)
115 if (optarg
!= NULL
) {
116 path_for_html
= optarg
;
122 if (optarg
!= NULL
) {
123 path_for_picture
= optarg
;
128 /* get amulesig path */
130 path
= get_amule_path("amulesig.dat", 1, config_path
);
133 perror("Unable to get aMule settings path\n");
137 /* open the file and if not exists exit with an error */
138 if ((amulesig
= fopen(path
, "r")) == NULL
) {
139 fprintf(stderr
, "Unable to open file %s\nCheck if you have amule online signature enabled.\n", path
);
142 /* i believe this shouldnt be here.
143 The freq of update could be higher than 60 seconds.
144 And it doesn't mean that the amule is not running.
149 if ( stat(path, &s_file) == 0 ) {
150 time_t t_now = time(0);
151 if ( (t_now - s_file.st_mtime) > 60 ) {
152 perror("aMule online signature last updated more then 60 sec ago\n");
153 perror("Check that your aMule is running\n");
159 /* initialize all the strings with nothing */
160 for (i
= 0; i
<= 19; i
++)
163 /* start reading the stuff from amulesign to the stats array */
165 fseek (amulesig
, 0 , SEEK_END
);
166 lSize
= ftell (amulesig
);
168 buffer
= (char*) malloc (lSize
);
169 if (buffer
== NULL
) {
170 perror("Could not create buffer\n");
173 fread (buffer
,1,lSize
,amulesig
);
176 stats
[0] = strtok (buffer
,"\n"); /* ed2k status*/
177 stats
[1] = strtok (NULL
,"\n"); /* server name */
178 stats
[2] = strtok (NULL
,"\n"); /* server ip */
179 stats
[3] = strtok (NULL
,"\n"); /* server port */
180 stats
[4] = strtok (NULL
,"\n"); /* high or low id */
181 stats
[5] = strtok (NULL
,"\n"); /* kad status */
182 stats
[6] = strtok (NULL
,"\n"); /* dl */
183 stats
[7] = strtok (NULL
,"\n"); /* ul*/
184 stats
[8] = strtok (NULL
,"\n"); /* queue*/
185 stats
[9] = strtok (NULL
,"\n"); /* clients*/
186 stats
[10] = strtok (NULL
,"\n"); /* nick*/
187 stats
[11] = strtok (NULL
,"\n"); /* total download */
188 stats
[12] = strtok (NULL
,"\n"); /* total upload */
189 stats
[13] = strtok (NULL
,"\n"); /* version */
190 stats
[14] = strtok (NULL
,"\n"); /*Session download*/
191 stats
[15] = strtok (NULL
,"\n"); /* Session upload */
192 stats
[16] = strtok (NULL
,"\n"); /* aMule running Time */
194 // local time stored as stats[17]
196 ltp
= localtime(<
);
197 strftime(arr
, 20, "%b %d %Y, %H:%M", ltp
);
199 // if amule isn't running say that and exit else print out the stuff
201 // if amule uptime is 0, then its not running...
202 if (strncmp(stats
[16],"0",1) == 0 ) {
203 perror("aMule is not running\n");
208 if (strncmp(stats
[0],"2",1) == 0)
209 CreateLine(lines
, 0 ,"aMule %s is connecting\n", stats
[13]);
211 CreateLine(lines
, 0, "aMule %s has been running for %s\n",
212 stats
[13], timeconv(stats
[16]));
216 if (strncmp(stats
[0],"0",1) == 0 && strncmp(stats
[5],"0",1) == 0)
217 CreateLine(lines
, 1, "%s is not connected ", stats
[10]);
218 else if (strncmp(stats
[0],"0",1) == 0 && strncmp(stats
[5],"0",1) != 0)
219 CreateLine(lines
, 1, "%s is connected to ", stats
[10]);
221 CreateLine(lines
, 1, "%s is connected to %s [%s:%s] with ", stats
[10],
222 stats
[1], stats
[2], stats
[3]);
225 if (strncmp(stats
[5],"2",1) == 0) {
226 if (strncmp(stats
[4],"H",1) == 0)
227 AppendToLine(lines
, 1, "HighID | Kad: ok \n");
228 else if (strncmp(stats
[4],"L",1) == 0)
229 AppendToLine(lines
, 1, "LowID | Kad: ok \n");
231 AppendToLine(lines
, 1, "Kad: ok \n");
232 } else if (strncmp(stats
[5],"1",1) == 0) {
233 if (strncmp(stats
[4],"H",1) == 0)
234 AppendToLine(lines
, 1, "HighID | Kad: firewalled \n");
235 else if (strncmp(stats
[4],"L",1) == 0)
236 AppendToLine(lines
, 1, "LowID | Kad: firewalled \n");
238 AppendToLine(lines
, 1, "Kad: firewalled \n");
240 if (strncmp(stats
[4],"H",1) == 0)
241 AppendToLine(lines
, 1, "HighID | Kad: off \n");
242 else if (strncmp(stats
[4],"L",1) == 0)
243 AppendToLine(lines
, 1, "LowID | Kad: off \n");
245 AppendToLine(lines
, 1, "but running\n");
248 stats
[11] = strdup(convbytes(stats
[11]));
249 stats
[12] = strdup(convbytes(stats
[12]));
251 CreateLine(lines
, 2, "Total Download: %s, Upload: %s\n",stats
[11] , stats
[12]);
253 stats
[15] = strdup(convbytes(stats
[15]));
254 stats
[14] = strdup(convbytes(stats
[14]));
256 CreateLine(lines
, 3, "Session Download: %s, Upload: %s\n",stats
[14], stats
[15]);
258 CreateLine(lines
, 4, "Download: %s kB/s, Upload: %s kB/s\n", stats
[6], stats
[7]);
260 CreateLine(lines
, 5, "Sharing: %s file(s), Clients on queue: %s\n", stats
[9] , stats
[8]);
262 CreateLine(lines
, 6, "Time: %s\n", arr
);
265 if (use_out_pic
== 1) {
266 if (!readconfig(&config
)) {
267 perror("Could not read config file\n");
271 if (!createimage(&config
, lines
, path_for_picture
)) {
272 perror("Could not create image!\n");
281 if (!readconfig(&config
)) {
282 perror("Could not read config file\n");
286 if (!create_html(stats
,lines
,config
.template, path_for_html
)) {
287 perror("Could not create the HTML Page.\n");
291 if (!createimage(&config
, lines
, path_for_picture
)) {
292 perror("Could not create image!\n");
300 for (i
= 0; i
<= 6; i
++) {
301 printf("%s", lines
[i
]);