Diff

Differences From Artifact [62178048bb]:

To Artifact [23ac99bb04]:


64
65
66
67
68
69
70
71
72
73
74










75
76

77

78
79
80








81
82
83

84
85

86

87

88
89
90
91
92
93
94
95
96
97
98
99
100

101




102
103
104

105
106


107

108
109
110
111
112
113
114
115
116
117
118

119

120
121













122


123
124
125
126
127
128
129
130
131
132
133



134
135
136
137
138
139
140
141
142
143


















144


















145
146
147



148
149

150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174

175
176
177
178
179
180



181
182
183
184
185
186
187
188
189
190

191
192
193
194
195
196




197
198
199
200
201









202
203
204

205
206






207






208
209






210
211
212








213









214

215







216
217

218
219
#ifndef NO_GETOPT
#include <getopt.h>
#endif
#define BARLEN 35
#define HEADERLEN 14
#define VERSION "0.8.10"


extern char *optarg;
extern int optind, opterr, optopt;











void bargraph(float percent, float secondper, char marks[BARLEN+HEADERLEN+1],int usefull) {
  char percentone[BARLEN+1], percenttwo[BARLEN+1], remain[BARLEN+1];

  unsigned int numberofmarks, numofmarkstwo, remainnum;

  numberofmarks=(int) ((float) (BARLEN*(percent/100)));
  if (!usefull) numofmarkstwo=(int) ((float) (BARLEN*(secondper/100))); else numofmarkstwo=(BARLEN-numberofmarks);
  remainnum=BARLEN-(numberofmarks+numofmarkstwo);








  memset(percentone, '#', numberofmarks);
  memset(percenttwo, '%', numofmarkstwo);
  memset(remain, '.', remainnum);

  percentone[numberofmarks]=0;
  percenttwo[numofmarkstwo]=0;

  remain[remainnum]=0;

  snprintf(marks, BARLEN+HEADERLEN+1, "%s\033[1;31m%s\033[1;37m%s", percentone, percenttwo, remain);

  return;
}

#ifndef HAVE_LIBSTATGRAB
/*
  Extracted alot from sysinfo.c (part of the procps package?) by
  Michael K. Johnson <johnsonm@redhat.com>
*/
unsigned long get_meminfo(char searchfor[12]) {
  FILE *fd;
  unsigned long value=0, results=0;
  char buffer[256]; /* Paranoia, reallly big buffer. */
  char feildname[12];

  if ((fd=fopen(PROC_MEMINFO, "r"))==NULL) return(0);




  while (!feof(fd)) {
    fgets(buffer, sizeof(buffer), fd);
    sscanf(buffer,"%11s%lu",feildname,&value);

    if (strcmp(feildname,searchfor)==0) results+=(value*1024);
  }


  fclose(fd);

  return(results);
}
#endif

int main(int argc, char *argv[]) {
  float percentram, percentswap, percentbuffer, percentused, percentfree;
  float ramfree, ramtotal, swapfree, swaptotal, doloop=0, cachedbuffer, totaltotal;
  char realbarchart[BARLEN+HEADERLEN+1], swapbarchart[BARLEN+HEADERLEN+1], totalbarchart[BARLEN+HEADERLEN+1];
  int i, dototals=0, doold=0, linestoup=2;
  unsigned long meminfo_cached, divisor=1024;
#ifndef HAVE_LIBSTATGRAB

  struct sysinfo sinfo;


  meminfo_cached=get_meminfo("Cached:");













#else


  sg_mem_stats *mem_stats=NULL;
  sg_swap_stats *swap_stats=NULL;
  struct systeminfo_st {
    unsigned long totalram;  /* Total usable main memory size */
    unsigned long freeram;   /* Available memory size */
    unsigned long sharedram; /* Amount of shared memory */
    unsigned long bufferram; /* Memory used by buffers */
    unsigned long totalswap; /* Total swap space size */
    unsigned long freeswap;  /* swap space still available */
  } sinfo;




  sg_init();

#ifdef HAVE_STATGRAB_DROP_PRIVILEGES
  /* Drop setuid/setgid privileges. */
  if (sg_drop_privileges() != 0) {
    perror("Error. Failed to drop privileges");
    return 1;
  }
#endif



















#endif


















#ifdef NO_GETOPT
  for (i=1;i<argc;i++) {
    if (argv[i][0]=='-') {



      switch(argv[i][1]) {
        case 's' : 

           if (argc>(i+1)) { doloop=atof(argv[i+1]); i++; }

           break;
#else
  while ((i=getopt(argc, argv, "obkmVs:t")) != -1) {
  if (i=='?') { printf("usage: %s [-b|-k|-m] [-o] [-t] [-s delay] [-V]\n",argv[0]); return(1); }
    switch(i) {
        case 's' : 
           doloop=atof(optarg);
           break;
#endif
        case 'b' : divisor=1; break;
        case 'k' : divisor=1024; break;
        case 'm' : divisor=1048576; break;
        case 'V' : printf("freecolor version %s\n", VERSION); return(0);
        case 't' : dototals=1; linestoup++; break;
        case 'o' : doold=1; linestoup++; break;
#ifdef NO_GETOPT

        default  : printf("%s: illegal option -- %c\n",argv[0],argv[i][1]); printf("usage: %s [-b|-k|-m] [-t] [-s delay] [-V]\n",argv[0]); return(1);
      }
    }
  }
#else
  	}
  }
#endif

  while(1) {
#ifndef HAVE_LIBSTATGRAB
    sysinfo(&sinfo);
#else
  if(((mem_stats=sg_get_mem_stats()) == NULL) || (swap_stats=sg_get_swap_stats()) == NULL) {
    printf("Couldn't get memory information, exiting.\n");



    return(-1);
  }
  meminfo_cached=mem_stats->cache;
  sinfo.totalram=mem_stats->total;
  sinfo.freeram=mem_stats->free;
  sinfo.sharedram=0;
  sinfo.bufferram=0;
  sinfo.totalswap=swap_stats->total;
  sinfo.freeswap=swap_stats->free;
#endif

    ramtotal=sinfo.totalram;
    cachedbuffer=sinfo.bufferram+meminfo_cached;
    ramfree=sinfo.freeram;
    swapfree=sinfo.freeswap;
    swaptotal=sinfo.totalswap;
    totaltotal=(sinfo.totalram+sinfo.totalswap);




    percentram=(float) ((ramfree / ramtotal)*100);
    percentbuffer=(float) ((cachedbuffer/ramtotal)*100);
    percentfree=(float) (((sinfo.freeram+sinfo.freeswap+meminfo_cached+sinfo.bufferram)/totaltotal)*100);
    percentused=(int) (100-((int) percentfree));
    if (!(swaptotal==0)) { percentswap=(float) ((swapfree/swaptotal)*100); } else { percentswap=0; }









    bargraph(percentswap,0,swapbarchart,0);
    bargraph(percentram,percentbuffer,realbarchart,0);
    bargraph((int) percentfree,(int) percentused,totalbarchart,1);

    if (!doold) {
      printf("Physical  : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n", realbarchart ,(int) (percentram+percentbuffer), (unsigned long) (ramfree+cachedbuffer)/divisor, (unsigned long) ramtotal/divisor);






      printf("Swap      : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n",swapbarchart ,(int) percentswap,(unsigned long) swapfree/divisor, (unsigned long) swaptotal/divisor);






      if (dototals) 
      printf("Total     : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m \033[0m(%lu=%lu+%lu)\n", totalbarchart, (unsigned long) (sinfo.totalram+sinfo.totalswap)/divisor,(unsigned long) ((sinfo.freeram+sinfo.freeswap)/divisor), (unsigned long) ((totaltotal-((sinfo.freeram+sinfo.freeswap+cachedbuffer)))/divisor));






    } else {
      printf("             total       used       free     shared    buffers     cached\n");
      printf("Mem:  %12lu %10lu %10lu %10lu %10lu %10lu\n",(sinfo.totalram/divisor), (sinfo.totalram-sinfo.freeram)/divisor, (sinfo.freeram/divisor), (sinfo.sharedram/divisor), (sinfo.bufferram/divisor), (((unsigned long) cachedbuffer)/divisor)); 








      printf("Swap: %12lu %10lu %10lu\n",(sinfo.totalswap/divisor), ((sinfo.totalswap-sinfo.freeswap)/divisor), (sinfo.freeswap/divisor));









      if (dototals) printf("Total: %11lu = (%8lu (used) + %8lu (free))\n",(long) totaltotal/divisor,(((long) totaltotal)-((sinfo.freeram+sinfo.freeswap)))/divisor,(sinfo.freeram+sinfo.freeswap+((unsigned long) cachedbuffer))/divisor);

    }







    if (doloop==0) { return(0); } else { usleep(doloop*1000000); printf("\033[%iA",linestoup); }
  }

  return(0);
}







<



>
>
>
>
>
>
>
>
>
>
|
<
>
|
>
|
<
<
>
>
>
>
>
>
>
>
|
|
|
>
|
|
>
|
>
|
>
|








|
|
|
|
>
|
>
>
>
>
|
|
|
>
|
|
>
>
|
>
|

<

<
<
<
<
<
<
<
>
|
>

|
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
|
|
<
<
<
<
<
<
<
<

>
>
>
|


|
|
|
|
|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
>
>
>
|
|
>
|
>
|

|
<
|
|
|
|
|
|
|
|
|
|
|
<
>
|
|
|
|
<
<
<
<
>
|
<
|
<
<
<
>
>
>
|
|
<
<
<
<
<
<
<
<
>
|
<
|
|
|
<
>
>
>
>
|
|
<
<
<
>
>
>
>
>
>
>
>
>
|
|
|
>
|
|
>
>
>
>
>
>
|
>
>
>
>
>
>
|
|
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
|
|
>
|

64
65
66
67
68
69
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84

85
86
87
88


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

139







140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162








163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

228
229
230
231
232
233
234
235
236
237
238

239
240
241
242
243




244
245

246



247
248
249
250
251








252
253

254
255
256

257
258
259
260
261
262



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef NO_GETOPT
#include <getopt.h>
#endif
#define BARLEN 35
#define HEADERLEN 14
#define VERSION "0.8.10"


extern char *optarg;
extern int optind, opterr, optopt;

struct freecolor_meminfo {
	unsigned long totalram;  /* Total usable main memory size */
	unsigned long freeram;   /* Available memory size */
	unsigned long sharedram; /* Amount of shared memory */
	unsigned long bufferram; /* Memory used by buffers */
	unsigned long cachedram; /* Memory used for I/O cache */
	unsigned long totalswap; /* Total swap space size */
	unsigned long freeswap;  /* swap space still available */
};

void bargraph(float percent, float secondper, char marks[BARLEN + HEADERLEN + 1], int usefull) {

	char percentone[BARLEN + 1], percenttwo[BARLEN + 1], remain[BARLEN + 1];
	unsigned int numberofmarks, numofmarkstwo, remainnum;

	numberofmarks=(int) ((float) (BARLEN * (percent / 100)));



	if (!usefull) {
		numofmarkstwo = (int) ((float) (BARLEN * (secondper / 100)));
	} else {
		numofmarkstwo = (BARLEN - numberofmarks);
	}

	remainnum = BARLEN - (numberofmarks + numofmarkstwo);
	memset(percentone, '#', numberofmarks);
	memset(percenttwo, '%', numofmarkstwo);
	memset(remain, '.', remainnum);

	percentone[numberofmarks] = 0;
	percenttwo[numofmarkstwo] = 0;

	remain[remainnum] = 0;

	snprintf(marks, BARLEN + HEADERLEN + 1, "%s\033[1;31m%s\033[1;37m%s", percentone, percenttwo, remain);

	return;
}

#ifndef HAVE_LIBSTATGRAB
/*
  Extracted alot from sysinfo.c (part of the procps package?) by
  Michael K. Johnson <johnsonm@redhat.com>
*/
unsigned long get_meminfo(char searchfor[12]) {
	FILE *fd;
	unsigned long value = 0, results = 0;
	char buffer[256];
	char fieldname[12];

	fd = fopen(PROC_MEMINFO, "r");
	if (fd == NULL) {
		return(0);
	}

	while (!feof(fd)) {
		fgets(buffer, sizeof(buffer), fd);
		sscanf(buffer,"%11s%lu",fieldname,&value);
		if (strcmp(fieldname, searchfor) == 0) {
			results+=(value*1024);
		}
	}

	fclose(fd);

	return(results);
}









int get_all_meminfo(struct freecolor_meminfo *retval) {
	struct sysinfo sinfo;
	unsigned long meminfo_cached;

	meminfo_cached = get_meminfo("Cached:");
	sysinfo(&sinfo);

	retval->totalram  = sinfo.totalram;
	retval->freeram   = sinfo.freeram;
	retval->sharedram = sinfo.sharedram;
	retval->bufferram = sinfo.bufferram;
	retval->totalswap = sinfo.totalswap;
	retval->freeswap  = sinfo.freeswap;

	retval->cachedram = meminfo_cached;

	return(0);
}
#else
int get_all_meminfo(struct freecolor_meminfo *retval) {
	static int init_called = 0;
	sg_mem_stats *mem_stats;
	sg_swap_stats *swap_stats;









	if (init_called == 0) {
		init_called = 1;

		sg_init();

#ifdef HAVE_STATGRAB_DROP_PRIVILEGES
		/* Drop setuid/setgid privileges. */
		if (sg_drop_privileges() != 0) {
			perror("Error. Failed to drop privileges");
			return(-1);
		}
#endif
	}

	mem_stats = sg_get_mem_stats();
	swap_stats = sg_get_swap_stats();

	if (mem_stats == NULL || swap_stats == NULL) {
		return(-1);
	}

	retval->totalram  = mem_stats->total;
	retval->freeram   = mem_stats->free;
	retval->cachedram = mem_stats->cache;
	retval->sharedram = 0;
	retval->bufferram = 0;
	retval->totalswap = swap_stats->total;
	retval->freeswap  = swap_stats->free;

	return(0);
}
#endif

int main(int argc, char **argv) {
	struct freecolor_meminfo sinfo;
	unsigned long cachedbuffer, divisor;
	float percentram, percentswap, percentbuffer, percentused, percentfree;
	float ramfree, ramtotal, swapfree, swaptotal, doloop, totaltotal;
	char realbarchart[BARLEN  +HEADERLEN + 1], swapbarchart[BARLEN + HEADERLEN + 1], totalbarchart[BARLEN + HEADERLEN + 1];
	int i, dototals, doold, linestoup;
	int gam_ret;

	/* Set defaults */
	divisor = 1024;
	doold = 0;
	linestoup = 2;
	dototals = 0;
	doloop = 0;

	/* Get arguments */
#ifdef NO_GETOPT
	for (i=1;i<argc;i++) {
		if (argv[i][0] != '-') {
			break;
		}

		switch(argv[i][1]) {
			case 's' : 
				if (argc > (i+1)) {
					doloop=atof(argv[i+1]); i++;
				}
				break;
#else
	while ((i=getopt(argc, argv, "obkmVs:t")) != -1) {

		switch(i) {
			case 's' : 
				doloop=atof(optarg);
				break;
#endif /* NO_GETOPT */
			case 'b' : divisor=1; break;
			case 'k' : divisor=1024; break;
			case 'm' : divisor=1048576; break;
			case 'V' : printf("freecolor version %s\n", VERSION); return(0);
			case 't' : dototals=1; linestoup++; break;
			case 'o' : doold=1; linestoup++; break;

			case '?' :  printf("usage: %s [-b|-k|-m] [-o] [-t] [-s delay] [-V]\n",argv[0]); return(1);
			default  : printf("%s: illegal option -- %c\n",argv[0],argv[i][1]); printf("usage: %s [-b|-k|-m] [-t] [-s delay] [-V]\n",argv[0]); return(1);
		}
  	}





	/* Display information */
	while(1) {

		gam_ret = get_all_meminfo(&sinfo);



		if (gam_ret < 0) {
			fprintf(stderr, "Error getting memory information.\n");

			return(1);
		}









		ramtotal  = sinfo.totalram;

		ramfree   = sinfo.freeram;
		swapfree  = sinfo.freeswap;
		swaptotal = sinfo.totalswap;


		totaltotal   = sinfo.totalram + sinfo.totalswap;
		cachedbuffer = sinfo.bufferram + sinfo.cachedram;

		percentram    = (float) ((ramfree / ramtotal) * 100);
		percentbuffer = (float) ((cachedbuffer / ramtotal) * 100);



		percentfree   = (float) (((sinfo.freeram + sinfo.freeswap + cachedbuffer) / totaltotal) * 100);
		percentused   = (int) (100 - ((int) percentfree));

		if (swaptotal!=0) {
			percentswap = (float) (( swapfree / swaptotal) * 100);
		} else {
			percentswap = 0;
		}

		bargraph(percentswap, 0, swapbarchart, 0);
		bargraph(percentram, percentbuffer, realbarchart, 0);
		bargraph((int) percentfree,(int) percentused, totalbarchart,1);

		if (!doold) {
			printf("Physical  : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n",
			    realbarchart,
			    (int) (percentram + percentbuffer),
			    (unsigned long) (ramfree + cachedbuffer) / divisor,
			    (unsigned long) ramtotal/divisor
			);

			printf("Swap      : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n",
			    swapbarchart,
			     (int) percentswap,
			     (unsigned long) swapfree / divisor,
			     (unsigned long) swaptotal/divisor
			);

			if (dototals) {
				printf("Total     : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m \033[0m(%lu=%lu+%lu)\n",
				    totalbarchart,
				    (unsigned long) (sinfo.totalram + sinfo.totalswap) / divisor,
				    (unsigned long) ((sinfo.freeram + sinfo.freeswap) / divisor),
				    (unsigned long) ((totaltotal - ((sinfo.freeram + sinfo.freeswap + cachedbuffer))) / divisor)
				);
			}
		} else {
			printf("             total       used       free     shared    buffers     cached\n");
			printf("Mem:  %12lu %10lu %10lu %10lu %10lu %10lu\n",
			    (unsigned long) (sinfo.totalram / divisor),
			    (unsigned long) ((sinfo.totalram - sinfo.freeram) / divisor),
			    (unsigned long) (sinfo.freeram / divisor),
			    (unsigned long) (sinfo.sharedram / divisor),
			    (unsigned long) (sinfo.bufferram / divisor),
			    (unsigned long) (cachedbuffer / divisor)
			); 

			printf("Swap: %12lu %10lu %10lu\n",
			    (unsigned long) (sinfo.totalswap / divisor),
			    (unsigned long) ((sinfo.totalswap - sinfo.freeswap) / divisor),
			    (unsigned long) (sinfo.freeswap / divisor)
			);

			if (dototals) {
				printf("Total: %11lu = (%8lu (used) + %8lu (free))\n",
				    (unsigned long) (totaltotal / divisor),
				    (((long) totaltotal) - ((sinfo.freeram+sinfo.freeswap))) / divisor,
				    (sinfo.freeram+sinfo.freeswap+((unsigned long) cachedbuffer)) / divisor
				);
			}
		}

		if (doloop==0) {
			break;
		}

		usleep(doloop*1000000);
		printf("\033[%iA",linestoup);
	}

	return(0);
}