5 #include <sys/utsname.h>
10 #include <libdockapp/wmgeneral.h>
12 #include "asmon-master.xpm"
13 #include "asmon-mask.xbm"
19 #define EXEC_ON_CLICK 1
20 #define ASMON_VERSION "0.72"
25 #define BCHAR_HEIGHT 9
42 /* Evil globals I haven't removed yet */
43 long last_pageins
=0, last_pageouts
=0;
44 long last_swapins
=0, last_swapouts
=0;
46 static int has_kern26
= 0;
53 void DrawUptime(void);
55 void printversion(void);
56 void asmon_routine(int Xpid
, int allmem
);
57 void DrawLite(int state
, int dx
, int dy
);
61 float DrawMemSwap(void);
62 extern int getLoad(float *);
63 extern int getSwap(unsigned long *, unsigned long *);
64 extern int getMem(unsigned long *, unsigned long *);
65 extern int getCPU(unsigned long *, unsigned long *,
66 unsigned long *, unsigned long *,
67 unsigned long *, unsigned long *,
68 unsigned long *, unsigned long *);
70 void DrawXmem(int Xpid
, float total
);
71 float DrawMemSwap(float total
, int allmem
);
74 int main(int argc
, char *argv
[])
82 int kernMajor
, kernMinor
, kernRev
;
86 if (strlen(ProgName
) >= 5)
87 ProgName
+= (strlen(ProgName
) - 5);
89 for (i
=1; i
<argc
; i
++) {
94 if (strcmp(arg
+1, "display")) {
102 strncpy( Command
, argv
[i
], 253);
103 strcat( Command
, " &");
116 fprintf(stderr
, "X Server memory stats unavailable for Solaris.\n");
130 if ( (fp
= fopen("/var/run/server.0.pid", "r")) != NULL
)
132 fscanf(fp
, " %d", &Xpid
);
135 if ( (fp
= fopen("/tmp/.X0-lock", "r")) != NULL
)
137 fscanf(fp
, " %d", &Xpid
);
145 /* Open 64x64 window */
146 openXwindow(argc
, argv
, asmon_master_xpm
, asmon_mask_bits
, asmon_mask_width
, asmon_mask_height
);
148 if( uname( &name
) != -1 )
150 if( strcmp( name
.sysname
, "Linux" ) == 0 )
152 sscanf(name
.release
, "%d.%d.%d", &kernMajor
, &kernMinor
,
154 if(( kernMajor
== 2 ) && ( kernMinor
== 6 ))
160 fprintf(stderr
, "Can't find system name\n");
164 asmon_routine(Xpid
, allmem
);
168 /**************************************************************************/
171 fprintf(stderr
, "\nasmon %s - by Brad Hall (bkh@rio.vg)\n\t\toriginally based on Timecop's wmcpu\n\n", ASMON_VERSION
);
172 fprintf(stderr
, "The top bar: left is the CPU usage, right is the load average\n");
173 fprintf(stderr
, "The middle bar: left memory usage devided by ticks into shared, buffers, and\n\t\t cached, respectively, and the number of megs used\n");
174 fprintf(stderr
, "The lower bar: the left swap usage and the number of megs swappedd avg\n");
175 fprintf(stderr
, "The bottom: the left is a set of LED's marking page's and swap's, the right is\n\t\t a bar representing the amount of memory that the X server \n\t\t is taking up, and the exact megs\n\n usage:\n");
176 fprintf(stderr
, "\t-display <display name>\n");
177 fprintf(stderr
, "\t-h\tthis screen\n");
178 fprintf(stderr
, "\t-v\tprint the version number\n");
180 fprintf(stderr
, "\t-u\tforce asmon to show uptime, rather than X mem use\n");
183 fprintf(stderr
, "\t-e cmd\texecute 'cmd' on mouse click\n");
185 fprintf(stderr
, "\n");
188 /**************************************************************************/
190 void printversion(void)
192 fprintf(stderr
, "asmon %s\n", ASMON_VERSION
);
195 /**************************************************************************/
197 void asmon_routine(int Xpid
, int allmem
)
199 int xpm_X
=0, xpm_Y
=0, count
=0;
208 /* Only run every 15 iterations */
209 if ( count
== 0 || count
== 15)
213 total
=DrawMemSwap(total
, allmem
);
219 /* X mem or Uptime? */
225 DrawXmem(Xpid
, total
);
230 RedrawWindowXY(xpm_X
, xpm_Y
);
231 while (XPending(display
))
233 XNextEvent(display
, &Event
);
239 fprintf(stderr
,"system(%s)\n",Command
);
241 if (Command
[ 0 ]) system(Command
);
245 RedrawWindowXY(xpm_X
, xpm_Y
);
248 XCloseDisplay(display
);
261 /**************************************************************************/
265 /* CPU Usage Meter */
271 unsigned long cpuIdle
, cpuUser
, cpuKern
, cpuWait
;
272 unsigned long pageIn
, pageOut
, swapIn
, swapOut
;
274 /* remember the statistics read last time */
275 static float cpustat
[4] = {0.0, 0.0, 0.0, 0.0};
276 float fields
[4] = {0.0, 0.0, 0.0, 0.0};
277 float cputotal
= 0.0;
279 getCPU(&cpuIdle
, &cpuUser
, &cpuKern
, &cpuWait
,
280 &pageIn
, &pageOut
, &swapIn
, &swapOut
);
282 // Calculate CPU stuff
283 fields
[0] = ((float)cpuIdle
- cpustat
[0]);
284 cpustat
[0] = (float)cpuIdle
;
285 cputotal
+= fields
[0];
287 fields
[1] = ((float)cpuUser
- cpustat
[1]);
288 cpustat
[1] = (float)cpuUser
;
289 cputotal
+= fields
[1];
291 fields
[2] = ((float)cpuKern
- cpustat
[2]);
292 cpustat
[2] = (float)cpuKern
;
293 cputotal
+= fields
[2];
295 fields
[3] = ((float)cpuWait
- cpustat
[3]);
296 cpustat
[3] = (float)cpuWait
;
297 cputotal
+= fields
[3];
302 cputotal
= ((cputotal
- (fields
[0] + fields
[2])) * 1.55);
303 if (cputotal
> 26) cputotal
= 26;
304 copyXPMArea(3, 84, cputotal
, 9, 5, 5);
305 copyXPMArea(15, 105, (27 - cputotal
), 9, (5 + cputotal
), 5);
306 copyXPMArea(16, 46, 2, 14, 32, 2);
310 if (pageIn
> last_pageins
) DrawLite(B_RED
, 5, 48);
311 else DrawLite(B_OFF
, 5, 48);
313 if (pageOut
> last_pageouts
) DrawLite(B_RED
, 10, 48);
314 else DrawLite(B_OFF
, 10, 48);
316 last_pageins
= pageIn
;
317 last_pageouts
= pageOut
;
320 if (swapIn
> last_swapins
) DrawLite(B_RED
, 5, 53);
321 else DrawLite(B_OFF
, 5, 53);
323 if (swapOut
> last_swapouts
) DrawLite(B_RED
, 10, 53);
324 else DrawLite(B_OFF
, 10, 53);
326 last_swapins
= swapIn
;
327 last_swapouts
= swapOut
;
332 /* CPU Usage Meter */
336 static double cpustat
[7]; /* remember the statistics read last time */
337 //double fields[7], info[7], cputotal=0.0,idlee=0.0;
338 double fields
[7], info
[7], cputotal
=0.0;
339 long pageins
=0, pageouts
=0, swapins
=0, swapouts
=0 ;
343 if( (fp
= fopen("/proc/stat", "r")) != NULL
)
348 fscanf(fp
, "cpu %lf %lf %lf %lf %lf %lf %lf", info
, info
+1,
349 info
+2, info
+3, info
+4, info
+5, info
+6);
353 if( (fp
= fopen("/proc/vmstat", "r")) != NULL
)
355 // gather data for LED's
356 while( fgets(buf
, 127, fp
) )
358 if (strstr(buf
, "pgpgin"))
359 sscanf(buf
, "pgpgin %ld", &pageins
);
361 if (strstr(buf
, "pgpgout"))
362 sscanf(buf
, "pgpgout %ld", &pageouts
);
364 if (strstr(buf
, "pswpin"))
365 sscanf(buf
, "pswpin %ld", &swapins
);
367 if (strstr(buf
, "pswpout"))
368 sscanf(buf
, "pswpout %ld", &swapouts
);
374 fscanf(fp
, "cpu %lf %lf %lf %lf", info
, info
+1, info
+2,
377 // gather data for LED's
378 while( fgets(buf
, 127, fp
) )
380 if (strstr(buf
, "page"))
381 sscanf(buf
, "page %ld %ld", &pageins
, &pageouts
);
383 if (strstr(buf
, "swap"))
384 sscanf(buf
, "swap %ld %ld", &swapins
, &swapouts
);
389 // Calculate CPU stuff
392 for(i
= 0; i
< 7; i
++)
394 fields
[i
] = info
[i
] - cpustat
[i
];
395 cputotal
+= fields
[i
];
396 cpustat
[i
] = info
[i
];
399 for(i
= 0; i
< 4; i
++)
401 fields
[i
] = info
[i
] - cpustat
[i
];
402 cputotal
+= fields
[i
];
403 cpustat
[i
] = info
[i
];
412 //cputotal = 100 * l1 ;
413 //cputotal=(100-(idlee*100/16))*26/100;
416 cputotal
= (cputotal
-(fields
[3]+fields
[4]))*1.55;
417 if ( cputotal
> 26 ) cputotal
= 26;
418 copyXPMArea(3,84,cputotal
,9,5,5);
419 copyXPMArea(15,105,(27-cputotal
),9,(5+cputotal
),5);
420 copyXPMArea(16,46,2,14,32,2);
424 if (pageins
> last_pageins
)
426 DrawLite(B_RED
, 5, 48);
428 DrawLite(B_OFF
, 5, 48);
431 if (pageouts
> last_pageouts
)
433 DrawLite(B_RED
, 10, 48);
435 DrawLite(B_OFF
, 10, 48);
437 last_pageins
= pageins
;
438 last_pageouts
= pageouts
;
441 if (swapins
> last_swapins
)
443 DrawLite(B_RED
, 5, 53);
445 DrawLite(B_OFF
, 5, 53);
448 if (swapouts
> last_swapouts
)
450 DrawLite(B_RED
, 10, 53);
452 DrawLite(B_OFF
, 10, 53);
454 last_swapins
= swapins
;
455 last_swapouts
= swapouts
;
461 /**************************************************************************/
467 static float oldv
= -1.0;
471 if (getLoad(&ftmp
) != -1)
475 if( (fp
= fopen("/proc/loadavg", "r")) != NULL
)
477 fscanf(fp
, "%f", &ftmp
);
483 tempa
=(ftmp
+0.005)*100;
485 copyXPMArea(3+(tempy
*6),66,6,9,50,5);
488 copyXPMArea(3+(tempy
*6),66,6,9,44,5);
489 copyXPMArea(65,66,3,9,41,5);
494 copyXPMArea(3+(tempy
*6),95,6,9,34,5);
496 copyXPMArea(3+(tempy
*6),66,6,9,34,5);
502 /**************************************************************************/
512 unsigned long memMax
, memFree
, swapMax
, swapFree
;
513 unsigned long MEMmem
, MEMswap
;
514 float memUsed
, swapUsed
;
517 getMem(&memMax
, &memFree
);
518 memUsed
= (float)(memMax
- memFree
);
520 getSwap(&swapMax
, &swapFree
);
521 swapUsed
= (float)(swapMax
- swapFree
);
528 if (((float)memMax
/ 1048576) >= 1)
529 MEMmem
= ((memUsed
* 31) / (float)memMax
);
531 MEMmem
= ((memUsed
* 36) / (float)memMax
);
535 copyXPMArea(4, 115, 55, 11, 4, 18);
538 copyXPMArea(3, 75, MEMmem
, 9, 5, 19);
539 copyXPMArea(15, 105, (36 - MEMmem
), 9, (5 + MEMmem
), 19);
541 tempa
= (memUsed
/ 1048576);
542 tempy
= (tempa
% 10);
543 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 50, 19);
544 tempy
= ((tempa
/ 10) % 10);
545 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 44, 19);
546 tempy
= ((tempa
/ 100) % 10);
549 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 38, 19);
550 copyXPMArea(16, 46, 2, 14, 35, 16);
554 copyXPMArea(16, 46, 2, 14, 41, 16);
558 copyXPMArea(4, 115, 55, 11, 4, 32);
565 if (((float)swapMax
/ 1048576) >= 1)
566 MEMswap
= ((swapUsed
* 31) / (float)swapMax
);
568 MEMswap
= ((swapUsed
* 36) / (float)swapMax
);
571 copyXPMArea(3, 75, MEMswap
, 9, 5, 33);
572 copyXPMArea(15, 105, (36 - MEMswap
), 9, (5 + MEMswap
), 33);
574 tempa
= (swapUsed
/ 1048576);
575 tempy
= (tempa
% 10);
576 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 50, 33);
577 tempy
= ((tempa
/ 10) % 10);
578 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 44, 33);
579 tempy
= ((tempa
/ 100) % 10);
582 copyXPMArea((3 + (tempy
* 6)), 66, 6, 9, 38, 33);
583 copyXPMArea(16, 46, 2, 14, 42, 16);
587 copyXPMArea(16, 46, 2, 14, 41, 30);
590 return (float)memMax
;
596 float DrawMemSwap(float total
, int allmem
)
599 if( (fp
= fopen("/proc/meminfo", "r")) != NULL
)
601 static float stotal
=0.0, sshared
=0.0, sbuffers
=0.0, scached
=0.0;
603 float used
, freeM
, shared
, buffers
, cached
, swaptotal
,
605 unsigned long MEMshar
,MEMbuff
, MEMswap
;
614 fgets(junk
, 120, fp
);
615 if (strstr(junk
, "MemTotal"))
617 sscanf(junk
, "MemTotal: %f kB", &scratch
);
618 total
= scratch
* 1024;
620 if (strstr(junk
, "MemFree"))
622 sscanf(junk
, "MemFree: %f kB", &scratch
);
623 freeM
= scratch
* 1024;
624 used
= total
- freeM
;
626 if (strstr(junk
, "Buffers"))
628 sscanf(junk
, "Buffers: %f kB", &scratch
);
629 buffers
= scratch
* 1024;
631 if (strstr(junk
, "Cached"))
633 sscanf(junk
, "Cached: %f kB", &scratch
);
634 cached
= scratch
* 1024;
636 if (strstr(junk
, "SwapTotal"))
638 sscanf(junk
, "SwapTotal: %f kB", &scratch
);
639 swaptotal
= scratch
* 1024;
641 if (strstr(junk
, "SwapFree"))
643 sscanf(junk
, "SwapFree: %f kB", &scratch
);
644 swapfreeM
= scratch
* 1024;
645 swapused
= swaptotal
- swapfreeM
;
650 fscanf(fp
, "Mem: %f %f %f %f %f %f\nSwap: %f %f %f", &total
,
651 &used
, &freeM
, &shared
, &buffers
, &cached
,
652 &swaptotal
, &swapused
, &swapfreeM
);
657 if(stotal
!= total
|| sshared
!= shared
|| sbuffers
!= buffers
|| scached
!= cached
)
659 stotal
= total
; sshared
= shared
; sbuffers
= buffers
; scached
= cached
;
660 if ( (total
/101048576) >= 1)
662 MEMshar
=((used
-buffers
-cached
)/total
)*27;
663 MEMbuff
=(buffers
/total
)*27;
665 MEMshar
=((used
-buffers
-cached
)/total
)*33;
666 MEMbuff
=(buffers
/total
)*33;
669 copyXPMArea(4, 115, 55, 11, 4, 18);
671 if ( (total
/101048576) >= 1)
673 copyXPMArea(3,75,((used
/total
)*28),9,5,19);
675 copyXPMArea(3,75,((used
/total
)*34),9,5,19);
678 copyXPMArea(15,105,1,9,5+MEMshar
,19);
679 copyXPMArea(15,105,1,9,7+MEMshar
+MEMbuff
,19);
680 copyXPMArea(15,105,(36-(used
/total
)*34),9,(5+(used
/total
)*34),19);
684 copyXPMArea(3+(tempy
*6),66,6,9,50,19);
686 copyXPMArea(3+(tempy
*6),66,6,9,44,19);
687 tempy
=(tempa
/100)%10;
688 if ( (total
/101048576) >= 1) {
689 copyXPMArea(3+(tempy
*6),66,6,9,38,19);
690 copyXPMArea(16,46,2,14,35,16);
692 copyXPMArea(16,46,2,14,41,16);
696 if ( swaptotal
== 0 ) MEMswap
= 0;
698 if ( (total
/101048576) >= 1)
699 MEMswap
=(swapused
*31)/swaptotal
;
700 else MEMswap
=(swapused
*36)/swaptotal
;
703 copyXPMArea(4, 115, 55, 11, 4, 32);
705 copyXPMArea(3,75,MEMswap
,9,5,33);
706 copyXPMArea(15,105,(36-(MEMswap
)),9,5+MEMswap
,33);
708 tempa
=swapused
/1048576;
710 copyXPMArea(3+(tempy
*6),66,6,9,50,33);
712 copyXPMArea(3+(tempy
*6),66,6,9,44,33);
715 copyXPMArea(3+(tempy
*6),66,6,9,38,33);
716 copyXPMArea(16,46,2,14,35,30);
718 copyXPMArea(16,46,2,14,41,30);
726 /**************************************************************************/
731 void DrawXmem(int Xpid
, float total
)
734 char buf
[128], XFileName
[256];
736 long old_Xsize
=-1, Xsize
=0;
738 sprintf(XFileName
, "/proc/%d/status", Xpid
);
740 if ((fp
= fopen(XFileName
, "r")) != NULL
)
742 while( fgets(buf
, 127, fp
) )
744 if (strstr(buf
, "VmSize"))
745 sscanf(buf
, "VmSize: %ld", &Xsize
);
749 int tempy
, tempa
, tempb
;
751 ratio
=Xsize
/(total
/1024);
752 if ( Xsize
> (total
/1024) ) Xsize
=total
/1024;
755 copyXPMArea(3+(tempy
*6),66,6,9,50,48);
761 copyXPMArea(3,84,((ratio
)*17),11,18,47);
762 copyXPMArea(15,105,(23-((ratio
)*17)),11,(18+(ratio
*22)),47);
763 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
764 copyXPMArea(3+(tempb
*6),66,6,9,38,48);
765 copyXPMArea(16,46,2,14,36,46);
767 copyXPMArea(3,84,((ratio
)*22),11,18,47);
768 copyXPMArea(15,105,(23-((ratio
)*22)),11,(18+(ratio
*22)),47);
769 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
770 copyXPMArea(16,46,2,14,41,46);
779 /**************************************************************************/
782 void DrawUptime(void)
784 int upt
, days
=0,hours
=0,mins
=0, old_mins
=-1,old_hours
=-1;
789 idUtmp
.ut_type
= BOOT_TIME
;
791 pUtmp
= getutid(&idUtmp
);
792 upt
= (time(0) - pUtmp
->ut_time
);
795 if( (fp
= fopen("/proc/uptime", "r")) != NULL
)
796 fscanf(fp
, "%d",&upt
);
810 copyXPMArea(20,105,36,9,18,48);
812 copyXPMArea(3+(tempy
*6),66,6,9,50,48);
814 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
815 copyXPMArea(63,66,3,9,41,48);
817 copyXPMArea(3+(tempy
*6),66,6,9,34,48);
819 copyXPMArea(3+(tempy
*6),66,6,9,28,48);
821 copyXPMArea(3+(tempy
*6),66,6,9,22,48);
824 copyXPMArea(3+(tempy
*6),66,6,9,50,48);
826 copyXPMArea(3+(tempy
*6),66,6,9,44,48);
827 copyXPMArea(63,66,3,9,41,48);
829 copyXPMArea(3+(tempy
*6),66,6,9,34,48);
831 copyXPMArea(3+(tempy
*6),66,6,9,28,48);
832 copyXPMArea(63,66,3,9,25,48);
834 copyXPMArea(3+(tempy
*6),66,6,9,18,48);
840 /**************************************************************************/
843 void DrawLite(int state
, int dx
, int dy
)
848 copyXPMArea(BREDX
, BREDY
, LITEW
, LITEH
, dx
, dy
);
851 copyXPMArea(BGREENX
, BGREENY
, LITEW
, LITEH
, dx
, dy
);
855 copyXPMArea(BOFFX
, BOFFY
, LITEW
, LITEH
, dx
, dy
);