return 0;
}
+
+struct iface_stats {
+ long int rx_bytes;
+ long int rx_packets;
+ long int tx_bytes;
+ long int tx_packets;
+};
+
+#define PROC_NETDEV "/proc/net/dev"
+
+static int get_iface_stats(const char *iface, struct iface_stats *stat)
+{
+ FILE *netdev;
+ char buf[1024];
+ char if_name[16];
+ char *str;
+ int error;
+
+ netdev = fopen(PROC_NETDEV, "r");
+
+ if (netdev == NULL) {
+ error = errno;
+ pr_err("Failed to open file %s: %d (%s)\n",
+ PROC_NETDEV, error, strerror(error));
+ return error;
+ }
+
+ while (fgets(buf, sizeof(buf), netdev)) {
+ get_word(buf, &str, if_name, sizeof(if_name));
+
+ /* Remove the ':' at the end of the if_name */
+ if_name[strlen(if_name) - 1] = 0;
+ if (strncmp(iface, if_name, sizeof(if_name)))
+ continue;
+
+ stat->rx_bytes = dec_to_longlong(str, &str);
+ stat->rx_packets = dec_to_longlong(str, &str);
+ /* errs */ dec_to_longlong(str, &str);
+ /* drop */ dec_to_longlong(str, &str);
+ /* fifo */ dec_to_longlong(str, &str);
+ /* frame */ dec_to_longlong(str, &str);
+ /* compressed */ dec_to_longlong(str, &str);
+ /* multicast */ dec_to_longlong(str, &str);
+ stat->tx_bytes = dec_to_longlong(str, &str);
+ stat->tx_packets = dec_to_longlong(str, &str);
+
+ pr_info("rx_b %ld rx_p %ld tx_b %ld tx_p %ld\n",
+ stat->rx_bytes, stat->rx_packets,
+ stat->tx_bytes, stat->tx_packets);
+
+ return 0;
+ }
+
+ pr_err("Interface %s not found\n", iface);
+ return -ENODEV;
+}
+
+int netstats_parser(char *rrd_data, void *parser_data)
+{
+ struct iface_stats stat;
+ char **iface_name = parser_data, *original = rrd_data;
+ int max_str = RRD_DATA_MAX_LEN;
+ int ret;
+
+ while(*iface_name) {
+ pr_info("getting data for iface %s\n", *iface_name);
+ ret = get_iface_stats(*iface_name, &stat);
+
+ if (!ret) {
+ ret = snprintf(rrd_data, max_str, "%ld:%ld:%ld:%ld",
+ stat.rx_bytes, stat.rx_packets,
+ stat.tx_bytes, stat.tx_packets);
+ } else {
+ ret = snprintf(rrd_data, max_str, "U:U:U:U");
+ }
+
+ max_str -= ret;
+ rrd_data += ret;
+
+ iface_name++;
+ if (!*iface_name)
+ break;
+
+ ret = snprintf(rrd_data, max_str, ":");
+ max_str -= ret;
+ rrd_data += ret;
+ }
+
+ printf("result: %s\n", original);
+ return 0;
+}