Advertisement
BobbyTables

tomatousb rstats

Jul 11th, 2011
2,339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 KB | None | 0 0
  1. /*
  2.     rstats modified for standalone use.
  3.    
  4.     Some code copied from tomatousb project
  5.     Copyright (C) 2006-2009 Jonathan Zarate
  6.  
  7.     Will read rstats backup file to JSON output.
  8. */
  9.  
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <sys/stat.h>
  16. #include <stdint.h>
  17. #include <inttypes.h>
  18.  
  19. #define K 1024
  20. #define M (1024 * 1024)
  21. #define G (1024 * 1024 * 1024)
  22.  
  23. #define MAX_NDAILY 62
  24. #define MAX_NMONTHLY 25
  25.  
  26. #define MAX_COUNTER 2
  27.  
  28. #define DAILY       0
  29. #define MONTHLY     1
  30.  
  31. #define ID_V0 0x30305352
  32. #define ID_V1 0x31305352
  33. #define CURRENT_ID ID_V1
  34.  
  35. typedef struct {
  36.     uint32_t xtime;
  37.     uint64_t counter[MAX_COUNTER];
  38. } data_t;
  39.  
  40. typedef struct {
  41.     uint32_t id;
  42.  
  43.     data_t daily[MAX_NDAILY];
  44.     int dailyp;
  45.  
  46.     data_t monthly[MAX_NMONTHLY];
  47.     int monthlyp;
  48. } history_t;
  49.  
  50. typedef struct {
  51.     uint32_t id;
  52.  
  53.     data_t daily[62];
  54.     int dailyp;
  55.  
  56.     data_t monthly[12];
  57.     int monthlyp;
  58. } history_v0_t;
  59.  
  60. history_t history;
  61.  
  62. const char uncomp_fn[] = "/var/tmp/rstats-uncomp";
  63.  
  64. int f_read(const char *path, void *buffer, int max)
  65. {
  66.     int f;
  67.     int n;
  68.  
  69.     if ((f = open(path, O_RDONLY)) < 0) return -1;
  70.     n = read(f, buffer, max);
  71.     close(f);
  72.     return n;
  73. }
  74.  
  75. int decomp(const char *fname, void *buffer, int size, int max)
  76. {
  77.     char s[256];
  78.     int n;
  79.  
  80.     unlink(uncomp_fn);
  81.  
  82.     n = 0;
  83.     sprintf(s, "gzip -dc %s > %s", fname, uncomp_fn);
  84.     if (system(s) == 0)
  85.     {
  86.         n = f_read(uncomp_fn, buffer, size * max);
  87.         if (n <= 0) n = 0;
  88.             else n = n / size;
  89.     }
  90.     else {
  91.         printf("%s: %s != 0\n", __FUNCTION__, s);
  92.     }
  93.     unlink(uncomp_fn);
  94.     memset((char *)buffer + (size * n), 0, (max - n) * size);
  95.     return n;
  96. }
  97.  
  98. static void clear_history(void)
  99. {
  100.     memset(&history, 0, sizeof(history));
  101.     history.id = CURRENT_ID;
  102. }
  103.  
  104. int load_history(const char *fname)
  105. {
  106.     history_t hist;
  107.  
  108.     if ((decomp(fname, &hist, sizeof(hist), 1) != 1) || (hist.id != CURRENT_ID)) {
  109.         history_v0_t v0;
  110.  
  111.         if ((decomp(fname, &v0, sizeof(v0), 1) != 1) || (v0.id != ID_V0)) {
  112.             return 0;
  113.         }
  114.         else {
  115.             // temp conversion
  116.             clear_history();
  117.  
  118.             // V0 -> V1
  119.             history.id = CURRENT_ID;
  120.             memcpy(history.daily, v0.daily, sizeof(history.daily));
  121.             history.dailyp = v0.dailyp;
  122.             memcpy(history.monthly, v0.monthly, sizeof(v0.monthly));    // v0 is just shorter
  123.             history.monthlyp = v0.monthlyp;
  124.         }
  125.     }
  126.     else {
  127.         memcpy(&history, &hist, sizeof(history));
  128.     }
  129.     return 1;
  130. }
  131.  
  132. int* get_time(int32_t xtime)
  133. {
  134.     int *i = malloc(sizeof(int) * 3);
  135.  
  136.     i[0] = ((xtime >> 16) & 0xFF) + 1900;
  137.     i[1] = ((xtime >> 8) & 0xFF) + 1;
  138.     i[2] = xtime & 0xFF;
  139.  
  140.     return i;
  141. }
  142.  
  143. void print_counters(data_t data, int div)
  144. {
  145.     int* time = get_time(data.xtime);
  146.    
  147.     printf("{\"date\":");
  148.     printf("\"%d/%d/%d\",", time[1], time[2], time[0]);
  149.     printf("\"download\":");
  150.     printf("\"%"PRIu64"\",", data.counter[0] / div);
  151.     printf("\"upload\":");
  152.     printf("\"%"PRIu64"\"", data.counter[1] / div);
  153.     printf("}");
  154. }
  155.  
  156. void print_json()
  157. {
  158.     int i, prnt = 0;
  159.  
  160.     printf("{\"daily\":[");
  161.     for(i = 0; i < MAX_NDAILY; i++)
  162.     {
  163.         data_t data = history.daily[i];
  164.  
  165.         if(data.xtime == 0) {
  166.             prnt = 0; continue;
  167.         }
  168.         else {
  169.             if(prnt) {
  170.                 printf(",");
  171.             }
  172.         }
  173.  
  174.         print_counters(data, M);
  175.         prnt = 1;
  176.     }
  177.     printf("],");
  178.  
  179.     printf("\"monthly\":[");
  180.     for(i = 0; i < MAX_NMONTHLY; i++)
  181.     {
  182.         data_t data = history.monthly[i];
  183.        
  184.         if(data.xtime == 0) {
  185.             prnt = 0; continue;
  186.         }
  187.         else {
  188.             if(prnt) {
  189.                 printf(",");
  190.             }
  191.         }
  192.  
  193.         print_counters(data, G);
  194.         prnt = 1;
  195.     }
  196.     printf("]}");
  197. }
  198.  
  199. int main(int argc, char *argv[])
  200. {
  201.     if (argc > 1)
  202.     {
  203.         load_history(argv[1]);
  204.         print_json();
  205.     }
  206.     else {
  207.         printf("rstats Copyright (C) 2006-2009 Jonathan Zarate\n");
  208.         printf("usage: <fname>\n");
  209.     }
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement