[c5c522c] | 1 | #include <stdlib.h> |
---|
| 2 | #include <stdio.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #ifdef IRIX |
---|
| 5 | #include <strings.h> |
---|
| 6 | #endif |
---|
| 7 | #include <unistd.h> |
---|
| 8 | #include <fcntl.h> |
---|
| 9 | #include <dirent.h> |
---|
| 10 | #include <sys/time.h> |
---|
| 11 | |
---|
| 12 | #include "../../shared/sockets.h" |
---|
| 13 | #include "../../shared/LL.h" |
---|
| 14 | |
---|
| 15 | #include "main.h" |
---|
| 16 | #include "mode.h" |
---|
| 17 | #include "mem.h" |
---|
| 18 | |
---|
| 19 | //the kewl utilisation info struct |
---|
| 20 | struct utilinfo { //utilisation |
---|
| 21 | unsigned long llUp; |
---|
| 22 | unsigned long llDown; |
---|
| 23 | unsigned long llUpSpeed; |
---|
| 24 | unsigned long llDownSpeed; |
---|
| 25 | }; |
---|
| 26 | |
---|
| 27 | int utilinfo_fd = 0; |
---|
| 28 | |
---|
| 29 | static char device[10]; |
---|
| 30 | |
---|
| 31 | //some boring function.. |
---|
| 32 | long tvdiff(struct timeval a,struct timeval b) { |
---|
| 33 | return (((b.tv_sec-a.tv_sec)*1000000)+(b.tv_usec-a.tv_usec)); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | //TODO: ugly function - know what a codingstandard is?? :-) |
---|
| 37 | static void get_util_info(struct utilinfo *result) |
---|
| 38 | { |
---|
| 39 | // int i, res; char *bufptr; |
---|
| 40 | static struct timeval tv,tvprev; |
---|
| 41 | static unsigned long long UpPrev,DownPrev; |
---|
| 42 | |
---|
| 43 | char intf[10]; |
---|
| 44 | unsigned long rbyt,rpac,rerr,rdrop,rfifo,rframe,rcom,rmulti=0; |
---|
| 45 | unsigned long tbyt,tpac,terr,tdrop,tfifo,tcolls,tcarr,tcom=0; |
---|
| 46 | char bagger[5000]; |
---|
| 47 | char *start; |
---|
| 48 | unsigned long diff; |
---|
| 49 | |
---|
| 50 | reread(utilinfo_fd, "get_utilinfo:"); |
---|
| 51 | start=NULL; |
---|
| 52 | if (buffer) start=strstr(buffer,device); |
---|
| 53 | |
---|
| 54 | if (start) { |
---|
| 55 | sscanf(start,"%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",intf,&rbyt,&rpac,&rerr,&rdrop,&rfifo,&rframe,&rcom,&rmulti, |
---|
| 56 | &tbyt,&tpac,&terr,&tdrop,&tfifo,&tcolls,&tcarr,&tcom); |
---|
| 57 | printf(" net=(%s,%lu,%lu)\n",intf,rbyt,tbyt); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | gettimeofday(&tv,NULL); |
---|
| 61 | diff=tvdiff(tvprev,tv); |
---|
| 62 | result->llUp=tbyt; |
---|
| 63 | result->llDown=rbyt; |
---|
| 64 | result->llUpSpeed=((tbyt-UpPrev)*1000000)/(diff); |
---|
| 65 | result->llDownSpeed=((rbyt-DownPrev)*1000000)/(diff); |
---|
| 66 | |
---|
| 67 | //debug info: |
---|
| 68 | printf(" net=(%s %lu %lu %lu %lu )\n",intf,result->llUp,result->llDown,result->llUpSpeed,result->llDownSpeed); |
---|
| 69 | |
---|
| 70 | tvprev=tv; |
---|
| 71 | UpPrev=tbyt; |
---|
| 72 | DownPrev=rbyt; |
---|
| 73 | /* result[0].total = getentry("MemTotal:", buffer); |
---|
| 74 | //result[0].free = getentry("MemFree:", buffer); |
---|
| 75 | re/sult[0].shared = getentry("MemShared:", buffer); |
---|
| 76 | result[0].buffers = getentry("Buffers:", buffer); |
---|
| 77 | result[0].cache = getentry("Cached:", buffer); |
---|
| 78 | result[1].total = getentry("SwapTotal:", buffer); |
---|
| 79 | result[1].free = getentry("SwapFree:", buffer); */ |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | int net_init() |
---|
| 84 | { |
---|
| 85 | if(!utilinfo_fd) |
---|
| 86 | { |
---|
| 87 | utilinfo_fd = open("/proc/net/dev",O_RDONLY); |
---|
| 88 | } |
---|
| 89 | strcpy(device,"eth0:"); |
---|
| 90 | return 0; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int net_close() |
---|
| 94 | { |
---|
| 95 | //i//f(uinfo_fd) |
---|
| 96 | // meminfo_fd = open("/proc/meminfo",O_RDONLY); |
---|
| 97 | close(utilinfo_fd); |
---|
| 98 | utilinfo_fd = 0; |
---|
| 99 | |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | ///////////////////////////////////////////////////////////////////////// |
---|
| 105 | // Mem Screen displays info about memory and swap usage... |
---|
| 106 | // |
---|
| 107 | int net_screen(int rep, int display) |
---|
| 108 | { |
---|
| 109 | int n; |
---|
| 110 | struct utilinfo util; |
---|
| 111 | static int first = 1; |
---|
| 112 | static int which_title=0; |
---|
| 113 | float value; |
---|
| 114 | |
---|
| 115 | if(first) |
---|
| 116 | { |
---|
| 117 | first = 0; |
---|
| 118 | |
---|
| 119 | sock_send_string(sock, "screen_add M\n"); |
---|
| 120 | sprintf(buffer, "screen_set M name {Memory & Swap: %s}\n", host); |
---|
| 121 | sock_send_string(sock, buffer); |
---|
| 122 | |
---|
| 123 | if(lcd_hgt >= 4) |
---|
| 124 | { |
---|
| 125 | sock_send_string(sock, "widget_add M title title\n"); |
---|
| 126 | sock_send_string(sock, "widget_set M title { MEM -==- SWAP}\n"); |
---|
| 127 | sock_send_string(sock, "widget_add M totl string\n"); |
---|
| 128 | sock_send_string(sock, "widget_add M used string\n"); |
---|
| 129 | sock_send_string(sock, "widget_set M totl 9 2 Totl\n"); |
---|
| 130 | sock_send_string(sock, "widget_set M used 9 3 Free\n"); |
---|
| 131 | sock_send_string(sock, "widget_add M EF string\n"); |
---|
| 132 | sock_send_string(sock, "widget_set M EF 1 4 {E F E F}\n"); |
---|
| 133 | sock_send_string(sock, "widget_add M memused string\n"); |
---|
| 134 | sock_send_string(sock, "widget_add M swapused string\n"); |
---|
| 135 | //sock_send_string(sock, "widget_set M memgauge 2 4 0\n"); |
---|
| 136 | //sock_send_string(sock, "widget_set M swapgauge 13 4 0\n"); |
---|
| 137 | } |
---|
| 138 | else |
---|
| 139 | { |
---|
| 140 | sock_send_string(sock, "widget_add M m string\n"); |
---|
| 141 | sock_send_string(sock, "widget_add M s string\n"); |
---|
| 142 | if(lcd_wid >= 20) |
---|
| 143 | { |
---|
| 144 | sock_send_string(sock, "widget_set M m 1 1 {M [ ]}\n"); |
---|
| 145 | sock_send_string(sock, "widget_set M s 1 2 {S [ ]}\n"); |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
| 149 | sock_send_string(sock, "widget_set M m 1 1 {M [ ]}\n"); |
---|
| 150 | sock_send_string(sock, "widget_set M s 1 2 {S [ ]}\n"); |
---|
| 151 | } |
---|
| 152 | sock_send_string(sock, "widget_add M mem% string\n"); |
---|
| 153 | sock_send_string(sock, "widget_add M swap% string\n"); |
---|
| 154 | sock_send_string(sock, "widget_set M mem% 16 1 { 0.0%}\n"); |
---|
| 155 | sock_send_string(sock, "widget_set M swap% 16 2 { 0.0%}\n"); |
---|
| 156 | |
---|
| 157 | //sock_send_string(sock, "widget_set M memgauge 8 1 0\n"); |
---|
| 158 | //sock_send_string(sock, "widget_set M swapgauge 8 2 0\n"); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | sock_send_string(sock, "widget_add M memtotl string\n"); |
---|
| 162 | sock_send_string(sock, "widget_add M swaptotl string\n"); |
---|
| 163 | |
---|
| 164 | sock_send_string(sock, "widget_add M memgauge hbar\n"); |
---|
| 165 | sock_send_string(sock, "widget_add M swapgauge hbar\n"); |
---|
| 166 | |
---|
| 167 | //sock_send_string(sock, "\n"); |
---|
| 168 | } //first |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | get_util_info(&util); |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | // flip the title back and forth... |
---|
| 175 | if(which_title & 4) |
---|
| 176 | { |
---|
| 177 | sprintf(buffer, "widget_set M title {%s}\n", host); |
---|
| 178 | sock_send_string(sock, buffer); |
---|
| 179 | } |
---|
| 180 | else sock_send_string(sock, "widget_set M title { MEM -==- SWAP}\n"); |
---|
| 181 | |
---|
| 182 | which_title = (which_title + 1)&7; |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | // Total memory |
---|
| 186 | //sprintf(tmp, "widget_set M memtotl 1 2 {%6dk}\n", mem[0].total); |
---|
| 187 | if(display) sock_send_string(sock, tmp); |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | return 0; |
---|
| 194 | } // End mem_screen() |
---|