source: src/router/rc/ttraff.c @ 9026

Last change on this file since 9026 was 9026, checked in by eko, 5 years ago

ttraff dies as midnight fix.

File size: 4.6 KB
Line 
1/* ttraff.c by Eko: 11.feb.2008
2 
3  used for collecting and storing WAN traffic info to nvram
4 
5*/
6
7#include <stdio.h>
8#include <time.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <bcmnvram.h>
12#include <cy_conf.h>
13#include <rc.h>
14#include <shutils.h>
15#include <syslog.h>
16#include <utils.h>
17#include <wlutils.h>
18
19
20unsigned long get_todays_rcvd (int day, int month, int year)
21{
22//fprintf (stderr, "entering get_todays_rcvd\n");
23char *next;
24char var[80];
25char tq[32];
26int i = 1;
27unsigned long rcvd = 0;
28
29  sprintf (tq, "traff-%02u-%u", month, year);
30  char *tdata = nvram_safe_get (tq);
31  if (tdata != NULL || strlen(tdata))
32   {
33    foreach (var, tdata, next)
34    {
35     if (i == day)
36            sscanf (var, "%lu:%*lu", &rcvd);
37         i++;
38    }
39   }
40//fprintf (stderr, "leaving get_todays_rcvd: rcvd=%lu\n", rcvd);
41  return rcvd;
42}
43
44unsigned long get_todays_sent (int day, int month, int year)
45{
46//fprintf (stderr, "entering get_todays_sent\n");
47char *next;
48char var[80];
49char tq[32];
50int i = 1;
51unsigned long sent = 0;
52
53  sprintf (tq, "traff-%02u-%u", month, year);
54  char *tdata = nvram_safe_get (tq);
55  if (tdata != NULL || strlen(tdata))
56   {
57    foreach (var, tdata, next)
58    {
59     if (i == day)
60            sscanf (var, "%*lu:%lu", &sent);
61         i++;
62    }
63   }
64//fprintf (stderr, "leaving get_todays_sent: sent=%lu\n", sent);
65  return sent;
66}
67
68int write_to_nvram (int day, int month, int year, unsigned long rcvd, unsigned long sent)
69{
70//fprintf (stderr, "entering write_to_nvram\n");
71char *next;
72char var[80];
73char tq[32];
74char temp[64] = "";
75char buffer[2048] = "";
76int i;
77int days = daysformonth (month, year);
78
79  sprintf (tq, "traff-%02u-%u", month, year);
80
81  for (i = 1; i <= days; i++)
82  {
83   if (i == day)         
84   {
85        sprintf (temp, "%lu:%lu", rcvd, sent);
86   }
87   else
88   {
89        sprintf (temp, "%lu:%lu", get_todays_rcvd (i, month, year) , get_todays_sent (i, month, year));
90   }
91   strcat (buffer, temp);
92   if (i < days) strcat (buffer, " ");
93  }
94 
95  nvram_set (tq, buffer);
96//fprintf (stderr, "leaving write_to_nvram\n");
97  return 1;
98}
99
100int
101ttraff_main (void)
102{
103
104  struct tm *currtime;
105  long tloc;
106
107  time (&tloc);         // get time in seconds since epoch
108  currtime = localtime (&tloc); // convert seconds to date structure
109 
110  while (currtime->tm_year < 100) //loop until ntp time is set (year >= 2000)
111  {
112   sleep (60);
113   time (&tloc);
114   currtime = localtime (&tloc);
115  }
116 
117/* now we have time, let's start */
118  static char wanface[32];
119  char line[256];
120  unsigned long in_dev = 0;
121  unsigned long out_dev = 0;
122  unsigned long in_diff = 0;
123  unsigned long out_diff = 0; 
124  unsigned long in_dev_last = 0;
125  unsigned long out_dev_last = 0;
126  int needcommit = 0;
127  int commited = 0;
128  int needbase = 1;
129  int day, month, year;
130 
131  strncpy (wanface, get_wan_face (), sizeof (wanface));
132
133
134/* now we can loop and collect data */
135
136  syslog (LOG_DEBUG, "ttraff: data collection started\n");
137
138  do
139  {
140   time (&tloc);
141   currtime = localtime (&tloc);
142   
143   day = currtime->tm_mday;
144   month = currtime->tm_mon + 1; // 1 - 12
145   year = currtime->tm_year + 1900;
146   
147 
148   FILE *in = fopen ("/proc/net/dev", "rb");
149
150    if (in != NULL)
151    {
152     while (fgets (line, sizeof (line), in) != NULL)
153     {
154      int ifl = 0;
155      if (!strchr (line, ':'))
156       continue;
157      while (line[ifl] != ':')
158       ifl++;
159      line[ifl] = 0;
160      if (strstr (line, wanface))
161      {
162          sscanf (line + ifl + 1,
163                      "%lu %*ld %*ld %*ld %*ld %*ld %*ld %*ld %lu %*ld %*ld %*ld %*ld %*ld %*ld %*ld",
164                      &in_dev,  &out_dev);
165      }
166     }
167   
168  fclose (in);
169    }
170   
171   if (needbase)
172   {
173    in_dev_last = in_dev;
174    out_dev_last = out_dev;
175    needbase = 0;
176    sleep (2);
177    continue;
178   }
179   
180   if (in_dev_last > in_dev || out_dev_last > out_dev)  // forget this data and get new base
181   {
182         needbase = 1;
183         sleep (2);
184         continue;
185   }
186   
187           
188   in_diff = (in_dev - in_dev_last) >> 20;  //MBytes
189   out_diff = (out_dev - out_dev_last) >> 20;  //MBytes
190   
191//fprintf (stderr, "in_diff=%lu, out_diff=%lu\n", in_diff, out_diff);
192 
193   if (in_diff || out_diff)
194   {
195    write_to_nvram (day, month, year, get_todays_rcvd (day, month, year) + in_diff, get_todays_sent (day, month, year) + out_diff);
196    in_dev_last = in_dev_last + (in_diff << 20);
197    out_dev_last = out_dev_last + (out_diff << 20);   
198   }
199   
200   if (currtime->tm_hour == 23 && currtime->tm_min == 59 && commited == 0)
201   {
202    needcommit = 1;
203   }
204   else
205   {
206        commited = 0;
207   }   
208     
209   if (needcommit)  //commit only 1 time per day (at 23:59)
210   {
211     nvram_commit();
212     commited = 1;
213     needcommit = 0;
214     syslog (LOG_DEBUG, "ttraff: data for %d-%d-%d commited to nvram\n", year, month, day);
215   }
216   
217   sleep (58);
218   
219  }
220  while (1);
221
222  return 0;
223
224}
Note: See TracBrowser for help on using the repository browser.