1/*
2 * Support for router statistics gathering
3 *
4 * Copyright (C) 2015, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * $Id: stats.c 364590 2012-10-24 18:17:31Z $
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <limits.h>
24#include <ctype.h>
25#include <string.h>
26#include <signal.h>
27#include <time.h>
28#include <unistd.h>
29#include <errno.h>
30
31#include <bcmnvram.h>
32#include <shutils.h>
33
34extern int http_post(const char *server, char *buf, size_t count);
35
36#define BUFSPACE MAX_NVRAM_SPACE	/* # of bytes of buffer */
37
38int
39http_stats(const char *url)
40{
41	char *buf, *s;
42	char **cur;
43	char *secrets[] = { "os_server", "stats_server", "http_passwd", NULL };
44	char *files[] = { "/proc/version", "/proc/meminfo", "/proc/cpuinfo", "/proc/interrupts",
45			  "/proc/net/dev", "/proc/net/pppoe", "/proc/net/snmp", NULL };
46	char *contents;
47
48	if (!(buf = malloc(BUFSPACE)))
49		return errno;
50
51	/* Get NVRAM variables */
52	nvram_getall(buf, BUFSPACE);
53	for (s = buf; *s; s++) {
54		for (cur = secrets; *cur; cur++) {
55			if (!strncmp(s, *cur, strlen(*cur))) {
56				s += strlen(*cur) + 1;
57				while (*s)
58					*s++ = '*';
59				break;
60			}
61		}
62		*(s += strlen(s)) = '&';
63	}
64
65	/* Dump interesting /proc entries */
66	for (cur = files; *cur; cur++) {
67		if ((contents = file2str(*cur))) {
68			s += snprintf(s, buf + BUFSPACE - s, "%s=%s&", *cur, contents);
69			free(contents);
70		}
71	}
72
73	/* Report uptime */
74	s += snprintf(s, buf + BUFSPACE - s, "uptime=%lu&", (unsigned long) time(NULL));
75
76	/* Save */
77	s += snprintf(s, buf + BUFSPACE - s, "action=save");
78	buf[BUFSPACE-1] = '\0';
79
80	/* Post to server */
81	http_post(url ? : nvram_safe_get("stats_server"), buf, BUFSPACE);
82
83	free(buf);
84	return 0;
85}
86