4 * Purpose: aMule Statistics
6 * Author: Pedro de Oliveira <falso@rdk.homeip.net>
8 * Copyright (c) 2004-2011 Pedro de Oliveira ( falso@rdk.homeip-net )
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"
42 #include "amule-config.h" // For HAVE_GETOPT_LONG
44 #ifndef HAVE_GETOPT_LONG
45 /* Code from getopt_long.h - getopt_long() for systems that lack it
46 Copyright (c) 2001-2011 Arthur de Jong, GPL 2 and later */
47 # define no_argument 0
48 # define required_argument 1
49 # define optional_argument 2
58 int getopt_long(int argc
,
60 const char *optstring
,
61 const struct option
*longopts
,
68 * ????.??.?? - falso: creation of cas.
69 * ????.??.?? - Jacobo221: Detect connecting state
70 * ????.??.?? - falso: HTML page generation
71 * 2004.08.27 - GonoszTopi: New line handling routines, to cope with lines
72 * longer than 80 characters. Fixes buffer overflow.
73 * 2005.12.10 - fulgas: added kad info support
74 * 2005,12,16 - stefanero: fixed Kad related stuff and some other things
77 static struct option long_options
[] = {
78 { "help", no_argument
, NULL
, 'h' },
79 { "html", optional_argument
, NULL
, 'p' },
80 { "picture", optional_argument
, NULL
, 'o' },
81 { "config-dir", required_argument
, NULL
, 'c' },
85 void usage(char *myname
)
87 printf (" ___ _ _ ___ c aMule statistics\n"
88 " /'___) /'_` )/',__) by Pedro de Oliveira\n"
89 "( (___ ( (_| |\\__, \\ <falso@rdk.homeip.net>\n"
90 "`\\____)`\\__,_)(____/ Version %s\n\n"
92 "Usage: %s [OPTION]\n"
93 "If run without any option prints stats to stdout\n\n"
96 "-o, --picture, -P\tWrites the online signature picture\n"
98 "-p, --html, -H\t\tHTML Page with stats and picture\n"
99 "-c, --config-dir\tSpecifies a config-dir different from home\n"
100 "-h, --help\t\tThis help you're reading\n", CAS_VERSION
, myname
);
103 #ifndef HAVE_GETOPT_LONG
105 /* Code from getopt_long.c - getopt_long() for systems that lack it
106 Copyright (c) 2001-2011 Arthur de Jong, GPL 2 and later
107 Slightly edited for the sake of clarity by Gaznevada */
109 int getopt_long(int argc
,
111 const char *optstring
,
112 const struct option
*longopts
,
115 if ( (optind
> 0) && (optind
< argc
) &&
116 (strncmp(argv
[optind
],"--",2) == 0) &&
117 (argv
[optind
][2] != '\0') ) {
119 for (i
= 0; longopts
[i
].name
!= NULL
; i
++) {
120 int length
= strlen(longopts
[i
].name
);
121 if (strncmp(argv
[optind
]+2,longopts
[i
].name
,length
) == 0) {
122 if ( (longopts
[i
].has_arg
== no_argument
) && (argv
[optind
][2+length
] == '\0') ) {
124 return longopts
[i
].val
;
126 else if ( (longopts
[i
].has_arg
== required_argument
) && (argv
[optind
][2+length
] == '=') ) {
127 optarg
=argv
[optind
]+3+length
;
129 return longopts
[i
].val
;
131 else if ( (longopts
[i
].has_arg
== required_argument
) && (argv
[optind
][2+length
] == '\0') ) {
132 optarg
=argv
[optind
+1];
134 return longopts
[i
].val
;
136 else if ( (longopts
[i
].has_arg
== optional_argument
) && (argv
[optind
][2+length
] == '=') ) {
137 optarg
=argv
[optind
]+3+length
;
139 return longopts
[i
].val
;
141 else if ( (longopts
[i
].has_arg
==optional_argument
) && (argv
[optind
][2+length
] == '\0') ) {
143 return longopts
[i
].val
;
148 return getopt(argc
,argv
,optstring
);
151 #endif // HAVE_GETOPT_LONG
153 int main(int argc
, char *argv
[])
155 /* Declaration of variables */
158 char *config_path
=NULL
;
161 char *lines
[IMG_TEXTLINES
];
169 char *path_for_picture
=NULL
;
171 char *path_for_html
=NULL
;
177 while ((c
= getopt_long (argc
, argv
, "c:P:H:hpo", long_options
, NULL
)) != -1)
190 if (optarg
!= NULL
) {
191 path_for_html
= optarg
;
198 if (optarg
!= NULL
) {
199 path_for_picture
= optarg
;
211 /* get amulesig path */
213 path
= get_amule_path("amulesig.dat", 1, config_path
);
216 perror("Unable to get aMule settings path\n");
220 /* open the file and if not exists exit with an error */
221 if ((amulesig
= fopen(path
, "r")) == NULL
) {
222 fprintf(stderr
, "Unable to open file %s\nCheck if you have amule online signature enabled.\n", path
);
225 /* i believe this shouldnt be here.
226 The freq of update could be higher than 60 seconds.
227 And it doesn't mean that the amule is not running.
232 if ( stat(path, &s_file) == 0 ) {
233 time_t t_now = time(0);
234 if ( (t_now - s_file.st_mtime) > 60 ) {
235 perror("aMule online signature last updated more then 60 sec ago\n");
236 perror("Check that your aMule is running\n");
242 /* initialize all the strings with nothing */
243 for (i
= 0; i
<= 19; i
++)
246 /* start reading the stuff from amulesign to the stats array */
248 fseek (amulesig
, 0 , SEEK_END
);
249 lSize
= ftell (amulesig
);
251 perror("aMule signature file is 0 Byte, exiting.\n");
255 buffer
= (char*) malloc (lSize
);
256 if (buffer
== NULL
) {
257 perror("Could not create buffer\n");
260 if (fread(buffer
,1,lSize
,amulesig
)){} // // prevent GCC warning
263 stats
[0] = strtok (buffer
,"\n");
265 stats
[i
] = strtok (NULL
,"\n");
266 if (NULL
== stats
[i
]) {
267 perror("Too few fields in aMule signature file, exiting.\n");
272 // local time stored as stats[17]
274 ltp
= localtime(<
);
275 strftime(arr
, 20, "%b %d %Y, %H:%M", ltp
);
277 // if amule isn't running say that and exit else print out the stuff
279 // if amule uptime is 0, then its not running...
280 if (strncmp(stats
[16],"0",1) == 0 ) {
281 perror("aMule is not running\n");
286 if (strncmp(stats
[0],"2",1) == 0)
287 CreateLine(lines
, 0 ,"aMule %s is connecting\n", stats
[13]);
289 CreateLine(lines
, 0, "aMule %s has been running for %s\n",
290 stats
[13], timeconv(stats
[16]));
294 if (strncmp(stats
[0],"0",1) == 0 && strncmp(stats
[5],"0",1) == 0)
295 CreateLine(lines
, 1, "%s is not connected ", stats
[10]);
296 else if (strncmp(stats
[0],"0",1) == 0 && strncmp(stats
[5],"0",1) != 0)
297 CreateLine(lines
, 1, "%s is connected to ", stats
[10]);
299 CreateLine(lines
, 1, "%s is connected to %s [%s:%s] with ", stats
[10],
300 stats
[1], stats
[2], stats
[3]);
303 if (strncmp(stats
[5],"2",1) == 0) {
304 if (strncmp(stats
[4],"H",1) == 0)
305 AppendToLine(lines
, 1, "HighID | Kad: ok \n");
306 else if (strncmp(stats
[4],"L",1) == 0)
307 AppendToLine(lines
, 1, "LowID | Kad: ok \n");
309 AppendToLine(lines
, 1, "Kad: ok \n");
310 } else if (strncmp(stats
[5],"1",1) == 0) {
311 if (strncmp(stats
[4],"H",1) == 0)
312 AppendToLine(lines
, 1, "HighID | Kad: firewalled \n");
313 else if (strncmp(stats
[4],"L",1) == 0)
314 AppendToLine(lines
, 1, "LowID | Kad: firewalled \n");
316 AppendToLine(lines
, 1, "Kad: firewalled \n");
318 if (strncmp(stats
[4],"H",1) == 0)
319 AppendToLine(lines
, 1, "HighID | Kad: off \n");
320 else if (strncmp(stats
[4],"L",1) == 0)
321 AppendToLine(lines
, 1, "LowID | Kad: off \n");
323 AppendToLine(lines
, 1, "but running\n");
326 stats
[11] = strdup(convbytes(stats
[11]));
327 stats
[12] = strdup(convbytes(stats
[12]));
329 CreateLine(lines
, 2, "Total Download: %s, Upload: %s\n",stats
[11] , stats
[12]);
331 stats
[15] = strdup(convbytes(stats
[15]));
332 stats
[14] = strdup(convbytes(stats
[14]));
334 CreateLine(lines
, 3, "Session Download: %s, Upload: %s\n",stats
[14], stats
[15]);
336 CreateLine(lines
, 4, "Download: %s kB/s, Upload: %s kB/s\n", stats
[6], stats
[7]);
338 CreateLine(lines
, 5, "Sharing: %s file(s), Clients on queue: %s\n", stats
[9] , stats
[8]);
340 CreateLine(lines
, 6, "Time: %s\n", arr
);
343 if (use_out_pic
== 1) {
344 if (!readconfig(&config
)) {
345 perror("Could not read config file\n");
349 if (!createimage(&config
, lines
, path_for_picture
)) {
350 perror("Could not create image!\n");
359 if (!readconfig(&config
)) {
360 perror("Could not read config file\n");
364 if (!create_html(stats
,lines
,config
.template, path_for_html
)) {
365 perror("Could not create the HTML Page.\n");
369 if (!createimage(&config
, lines
, path_for_picture
)) {
370 perror("Could not create image!\n");
378 for (i
= 0; i
<= 6; i
++) {
379 printf("%s", lines
[i
]);