Пример #1
0
LxQtCpuLoad::LxQtCpuLoad(ILxQtPanelPlugin* plugin, QWidget* parent):
    QFrame(parent),
    mPlugin(plugin),
	m_showText(false),
    m_barOrientation(TopDownBar),
    m_timerID(-1)
{
    setObjectName("LxQtCpuLoad");

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(&m_stuff);

	/* Initialise statgrab */
#ifdef STATGRAB_NEWER_THAN_0_90
	sg_init(0);
#else
	sg_init();
#endif

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != 0) {
		perror("Error. Failed to drop privileges");
	}

	m_font.setPointSizeF(8);

	settingsChanged();
}
Пример #2
0
static int do_init(void) {
#if HAVE_SETLOCALE
  if (setlocale(LC_NUMERIC, COLLECTD_LOCALE) == NULL)
    WARNING("setlocale (\"%s\") failed.", COLLECTD_LOCALE);

  /* Update the environment, so that libraries that are calling
   * setlocale(LC_NUMERIC, "") don't accidentally revert these changes. */
  unsetenv("LC_ALL");
  setenv("LC_NUMERIC", COLLECTD_LOCALE, /* overwrite = */ 1);
#endif

#if HAVE_LIBKSTAT
  kc = NULL;
  update_kstat();
#endif

#if HAVE_LIBSTATGRAB
  if (sg_init(
#if HAVE_LIBSTATGRAB_0_90
          0
#endif
          )) {
    ERROR("sg_init: %s", sg_str_error(sg_get_error()));
    return -1;
  }

  if (sg_drop_privileges()) {
    ERROR("sg_drop_privileges: %s", sg_str_error(sg_get_error()));
    return -1;
  }
#endif

  return plugin_init_all();
} /* int do_init () */
int main(int argc, char **argv){

	sg_network_iface_stats *network_iface_stats;
	size_t iface_count, i;

	/* Initialise helper - e.g. logging, if any */
	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

	/* Initialise statgrab */
	sg_init(1);

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != SG_ERROR_NONE)
		sg_die("Error. Failed to drop privileges", 1);

	network_iface_stats = sg_get_network_iface_stats(&iface_count);
	if(network_iface_stats == NULL)
		sg_die("Failed to get network interface stats", 1);

	if (argc != 1) {
		/* If an argument is given, use bsearch to find just that
		 * interface. */
		sg_network_iface_stats key;

		key.interface_name = argv[1];
		network_iface_stats = bsearch(&key, network_iface_stats,
		                              iface_count,
		                              sizeof *network_iface_stats,
		                              sg_network_iface_compare_name);
		if (network_iface_stats == NULL) {
			fprintf(stderr, "Interface %s not found\n", argv[1]);
			exit(1);
		}
		iface_count = 1;
	}

	printf("Name\tSpeed\tDuplex\n");
	for(i = 0; i < iface_count; i++) {
		printf("%s\t%llu\t", network_iface_stats->interface_name, network_iface_stats->speed);
		switch (network_iface_stats->duplex) {
		case SG_IFACE_DUPLEX_FULL:
			printf("full\n");
			break;
		case SG_IFACE_DUPLEX_HALF:
			printf("half\n");
			break;
		default:
			printf("unknown\n");
			break;
		}
		network_iface_stats++;
	}

	exit(0);
}
Пример #4
0
/*
 * call-seq:
 *   Statgrab.new(drop_privileges=true)
 *
 * Set privileges and prepare connections, and then drop privileges.
 * See <tt>statgrab(3)</tt> manpage.
 */
static VALUE
statgrab_initialize(VALUE self, VALUE args)
{
	if (sg_init())
		statgrab_handle_error();

	if (rb_ary_shift(args) != Qfalse && sg_drop_privileges())
		statgrab_handle_error();

	return self;
}
Пример #5
0
int main(int argc, char **argv){

	extern char *optarg;
	int c;

	int delay = 1;
	sg_page_stats *page_stats;

	while ((c = getopt(argc, argv, "d:")) != -1){
		switch (c){
			case 'd':
				delay = atoi(optarg);
				break;
		}
	}

	/* Initialise helper - e.g. logging, if any */
	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

	/* Initialise statgrab */
	sg_init(1);

	register_sig_flagger( SIGINT, &quit );

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != SG_ERROR_NONE)
		sg_die("Error. Failed to drop privileges", 1);

	page_stats = sg_get_page_stats_diff(NULL);
	if(page_stats == NULL)
		sg_die("Failed to get page stats", 1);

	while( (page_stats = sg_get_page_stats_diff(NULL)) != NULL){
		int ch;
		printf("Pages in : %llu\n", page_stats->pages_pagein);
		printf("Pages out : %llu\n", page_stats->pages_pageout);
		ch = inp_wait(delay);
		if( quit || (ch == 'q') )
			break;
	}

	exit(0);
}
Пример #6
0
RazorCpuLoad::RazorCpuLoad(const RazorPanelPluginStartInfo* startInfo, QWidget* parent):
	RazorPanelPlugin(startInfo, parent),
	m_showText(false),
    m_timerID(-1)
{
	setObjectName("CpuLoad");
	addWidget(&m_stuff);

	/* Initialise statgrab */
	sg_init();

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != 0) {
		perror("Error. Failed to drop privileges");
	}

	m_font.setPointSizeF(8);

	settingsChanged();
}
Пример #7
0
int main(int argc, char **argv){

	sg_mem_stats *mem_stats;
	sg_swap_stats *swap_stats;

	long long total, free;

	/* Initialise helper - e.g. logging, if any */
	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

	/* Initialise statgrab */
	sg_init(1);

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != SG_ERROR_NONE)
		sg_die("Error. Failed to drop privileges", 1);

	if( ((mem_stats = sg_get_mem_stats(NULL)) != NULL) &&
	    ((swap_stats = sg_get_swap_stats(NULL)) != NULL) ) {
		printf("Total memory in bytes : %llu\n", mem_stats->total);
		printf("Used memory in bytes : %llu\n", mem_stats->used);
		printf("Cache memory in bytes : %llu\n", mem_stats->cache);
		printf("Free memory in bytes : %llu\n", mem_stats->free);

		printf("Swap total in bytes : %llu\n", swap_stats->total);
		printf("Swap used in bytes : %llu\n", swap_stats->used);
		printf("Swap free in bytes : %llu\n", swap_stats->free);

		total = mem_stats->total + swap_stats->total;
		free = mem_stats->free + swap_stats->free;

		printf("Total VM usage : %5.2f%%\n", 100 - (((float)total/(float)free)));
	}
	else {
		sg_die("Unable to get VM stats", 1);
	}

	exit(0);
}
Пример #8
0
int main(int argc, char **argv) {
    sg_fs_stats *fs_stats;
    size_t fs_size;

    /* Initialise helper - e.g. logging, if any */
    sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

    /* Initialise statgrab */
    sg_init(1);

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

    fs_stats = sg_get_fs_stats(&fs_size);
    if(fs_stats == NULL)
        sg_die("Failed to get file systems snapshot", 1);

    printf( "%-16s %-24s %-8s %16s %16s %16s %7s %7s %7s %7s %9s %9s %7s %7s %7s %7s\n",
            "device", "mountpt", "fstype", "size", "used", "avail",
            "i-total", "i-used", "i-free", "i-avail",
            "io size", "block size",
            "b-total", "b-used", "b-free", "b-avail");

    for( ; fs_size > 0 ; --fs_size, ++fs_stats ) {
        printf( "%-16s %-24s %-8s %16llu %16llu %16llu %7llu %7llu %7llu %7llu %9llu %9llu %7llu %7llu %7llu %7llu\n",
                fs_stats->device_name, fs_stats->mnt_point, fs_stats->fs_type,
                fs_stats->size, fs_stats->used, fs_stats->avail,
                fs_stats->total_inodes, fs_stats->used_inodes, fs_stats->free_inodes, fs_stats->avail_inodes,
                fs_stats->io_size, fs_stats->block_size,
                fs_stats->total_blocks, fs_stats->used_blocks, fs_stats->free_blocks, fs_stats->avail_blocks);
    }

    return 0;
}
Пример #9
0
int main(int argc, char **argv) {
	opterr = 0;
	while (1) {
		int c = getopt(argc, argv, "lbmunsot:pf:KMG");
		if (c == -1)
			break;
		switch (c) {
		case 'l':
			display_mode = DISPLAY_LINUX;
			break;
		case 'b':
			display_mode = DISPLAY_BSD;
			break;
		case 'm':
			display_mode = DISPLAY_MRTG;
			break;
		case 'u':
			display_mode = DISPLAY_PLAIN;
			break;
		case 'n':
			repeat_mode = REPEAT_NONE;
			break;
		case 's':
			repeat_mode = REPEAT_FOREVER;
			break;
		case 'o':
			repeat_mode = REPEAT_ONCE;
			break;
		case 't':
			repeat_time = atoi(optarg);
			break;
		case 'p':
			use_cpu_percent = 1;
			break;
		case 'f':
			float_scale_factor = atol(optarg);
			break;
		case 'K':
			bytes_scale_factor = 1024;
			break;
		case 'M':
			bytes_scale_factor = 1024 * 1024;
			break;
		case 'G':
			bytes_scale_factor = 1024 * 1024 * 1024;
			break;
		default:
			usage();
		}
	}

	if (display_mode == DISPLAY_MRTG) {
		if ((argc - optind) != 2)
			die("mrtg mode: must specify exactly two stats");
		if (repeat_mode == REPEAT_FOREVER)
			die("mrtg mode: cannot repeat display");
	}

	if (use_cpu_percent && repeat_mode == REPEAT_NONE)
		die("CPU percentage usage display requires stat differences");

	if (repeat_mode == REPEAT_NONE)
		use_diffs = 0;
	else
		use_diffs = 1;

	select_interesting(argc - optind, &argv[optind]);

	/* We don't care if sg_init fails, because we can just display
 	   the statistics that can be read as non-root. */
	sg_init();
	sg_snapshot();
	if (sg_drop_privileges() != 0)
		die("Failed to drop setuid/setgid privileges");

	switch (repeat_mode) {
	case REPEAT_NONE:
		get_stats();
		print_stats(argc, argv);
		break;
	case REPEAT_ONCE:
		get_stats();
		sleep(repeat_time);
		sg_snapshot();
		get_stats();
		print_stats(argc, argv);
		break;
	case REPEAT_FOREVER:
		while (1) {
			get_stats();
			print_stats(argc, argv);
			printf("\n");
			sleep(repeat_time);
			sg_snapshot();
		}
	}

	if (display_mode == DISPLAY_MRTG) {
		printf("\n");
		printf("statgrab\n");
	}

	sg_shutdown();

	return 0;
}
Пример #10
0
int main(int argc, char **argv){
	int c;
	int colouron = 0;

	char *fslist = NULL;

	time_t last_update = 0;

	extern int errno;

	int delay=2;

	sg_log_init("saidar", "SAIDAR_LOG_PROPERTIES", argc ? argv[0] : NULL);
	sg_init(1);
	if(sg_drop_privileges() != 0){
		fprintf(stderr, "Failed to drop setuid/setgid privileges\n");
		return 1;
	}

#ifdef COLOR_SUPPORT
	while ((c = getopt(argc, argv, "d:F:cvh")) != -1){
#else
	while ((c = getopt(argc, argv, "d:F:vh")) != -1){
#endif
		switch (c){
			case 'd':
				delay = atoi(optarg);
				if (delay < 1){
					fprintf(stderr, "Time must be 1 second or greater\n");
					exit(1);
				}
				break;
#ifdef COLOR_SUPPORT
			case 'c':
				colouron = 1;
				break;
#endif
			case 'v':
				version_num(argv[0]);
				break;
			case 'h':
			default:
				usage(argv[0]);
				return 1;
		}
	}

	if (fslist) {
		sg_error rc = set_valid_filesystems(fslist);
		if(rc != SG_ERROR_NONE)
			die(sg_str_error(rc));
		free(fslist);
	}
	else {
		sg_error rc = set_valid_filesystems("!nfs, nfs3, nfs4, cifs, smbfs, samba, autofs");
		if(rc != SG_ERROR_NONE)
			die(sg_str_error(rc));
	}

	signal(SIGWINCH, sig_winch_handler);
	initscr();
#ifdef COLOR_SUPPORT
	/* turn on colour */
	if (colouron) {
		if (has_colors()) {
			start_color();
			use_default_colors();
			init_pair(1,COLOR_RED,-1);
			init_pair(2,COLOR_GREEN,-1);
			init_pair(3,COLOR_YELLOW,-1);
			init_pair(4,COLOR_BLUE,-1);
			init_pair(5,COLOR_MAGENTA,-1);
			init_pair(6,COLOR_CYAN,-1);
		} else {
			fprintf(stderr, "Colour support disabled: your terminal does not support colour.");
			colouron = 0;
		}
	}
#endif
	nonl();
	curs_set(0);
	cbreak();
	noecho();
	timeout(delay * 1000);
	newwin(0, 0, 0, 0);
	clear();

	if(!get_stats()){
		fprintf(stderr, "Failed to get all the stats. Please check correct permissions\n");
		endwin();
		return 1;
	}

	display_headings();

	for(;;){
		time_t now;
		int ch = getch();

		if (ch == 'q'){
			break;
		}

		/* To keep the numbers slightly accurate we do not want them
		 * updating more frequently than once a second.
		 */
		now = time(NULL);
		if ((now - last_update) >= 1) {
			get_stats();
		}
		last_update = now;

		if(sig_winch_flag) {
			clear();
			display_headings();
			sig_winch_flag = 0;
		}

		display_data(colouron);
	}

	endwin();
	sg_shutdown();
	return 0;
}
Пример #11
0
int main(int argc, char **argv) {

    extern char *optarg;
    int c;

    /* We default to 1 second updates and displaying in bytes*/
    int delay = 1;
    char units = 'b';

    sg_disk_io_stats *diskio_stats;
    int num_diskio_stats;

    /* Parse command line options */
    while ((c = getopt(argc, argv, "d:bkm")) != -1) {
        switch (c) {
        case 'd':
            delay =	atoi(optarg);
            break;
        case 'b':
            units = 'b';
            break;
        case 'k':
            units = 'k';
            break;
        case 'm':
            units = 'm';
            break;
        }
    }

    /* Initialise statgrab */
    sg_init();

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

    /* We are not interested in the amount of traffic ever transmitted, just differences.
     * Because of this, we do nothing for the very first call.
     */

    diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats);
    if (diskio_stats == NULL) {
        perror("Error. Failed to get disk stats");
        return 1;
    }

    /* Clear the screen ready for display the disk stats */
    printf("\033[2J");

    /* Keep getting the disk stats */
    while ( (diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats)) != NULL) {
        int x;
        int line_number = 2;

        long long total_write=0;
        long long total_read=0;

        for(x = 0; x < num_diskio_stats; x++) {
            /* Print at location 2, linenumber the interface name */
            printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", diskio_stats->disk_name);
            /* Print out at the correct location the traffic in the requsted units passed at command time */
            switch(units) {
            case 'b':
                printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk read", diskio_stats->read_bytes);
                printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk write", diskio_stats->write_bytes);
                break;
            case 'k':
                printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk read", (diskio_stats->read_bytes / 1024));
                printf("\033[%d;2H%-25s : %5lld", line_number++, "Disk write", (diskio_stats->write_bytes / 1024));
                break;
            case 'm':
                printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk read", diskio_stats->read_bytes / (1024.00*1024.00));
                printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk write", diskio_stats->write_bytes / (1024.00*1024.00));
            }
            printf("\033[%d;2H%-25s : %ld ", line_number++, "Disk systime", (long) diskio_stats->systime);

            /* Add a blank line between interfaces */
            line_number++;

            /* Add up this interface to the total so we can display a "total" disk io" */
            total_write+=diskio_stats->write_bytes;
            total_read+=diskio_stats->read_bytes;

            /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need
             * to keep track of the orginal pointer to free later */
            diskio_stats++;
        }

        printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", "Total Disk IO");
        switch(units) {
        case 'b':
            printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk Total read", total_read);
            printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk Total write", total_write);
            break;
        case 'k':
            printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk Total read", (total_read / 1024));
            printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk Total write", (total_write / 1024));
            break;
        case 'm':
            printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total read", (total_read  / (1024.00*1024.00)));
            printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total write", (total_write  / (1024.00*1024.00)));
            break;
        }

        fflush(stdout);

        sleep(delay);

    }

    return 0;

}
Пример #12
0
int main(int argc, char **argv){

	extern char *optarg;
	int c;

	int delay = 1;
	sg_cpu_percents *cpu_percent;
	sg_cpu_stats *cpu_diff_stats;

	while ((c = getopt(argc, argv, "d:")) != -1){
		switch (c){
			case 'd':
				delay = atoi(optarg);
				break;
		}
	}
#ifdef WIN32
	delay = delay * 1000;
#endif

	/* Initialise helper - e.g. logging, if any */
	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

	/* Initialise statgrab */
	sg_init(1);
	/* XXX must be replaced by termios/(n)curses function ....
	if( 0 != setvbuf(stdin, NULL, _IONBF, 0) ) {
		perror("setvbuf");
		exit(1);
	} */

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != SG_ERROR_NONE) {
		sg_die("Error. Failed to drop privileges", 1);
	}

	register_sig_flagger( SIGINT, &quit );

	/* Throw away the first reading as thats averaged over the machines uptime */
	sg_snapshot();
	cpu_percent = sg_get_cpu_percents(NULL);
	if( NULL == cpu_percent )
		sg_die("Failed to get cpu stats", 1);

	/* Clear the screen ready for display the cpu usage */
	printf("\033[2J");

	while( ( ( cpu_diff_stats = sg_get_cpu_stats_diff(NULL) ) != NULL ) &&
	     ( ( cpu_percent = sg_get_cpu_percents_of(sg_last_diff_cpu_percent, NULL) ) != NULL ) ) {
		int ch;
		sg_snapshot();
		printf("\033[2;2H%-14s : %lld (%6.2f)", "User CPU", cpu_diff_stats->user, cpu_percent->user);
		printf("\033[3;2H%-14s : %lld (%6.2f)", "Kernel CPU", cpu_diff_stats->kernel, cpu_percent->kernel);
		printf("\033[4;2H%-14s : %lld (%6.2f)", "IOWait CPU", cpu_diff_stats->iowait, cpu_percent->iowait);
		printf("\033[5;2H%-14s : %lld (%6.2f)", "Swap CPU", cpu_diff_stats->swap, cpu_percent->swap);
		printf("\033[6;2H%-14s : %lld (%6.2f)", "Nice CPU", cpu_diff_stats->nice, cpu_percent->nice);
		printf("\033[7;2H%-14s : %lld (%6.2f)", "Idle CPU", cpu_diff_stats->idle, cpu_percent->idle);
		printf("\033[8;2H%-14s : %llu", "Ctxts", cpu_diff_stats->context_switches);
		printf("\033[9;2H%-14s : %llu", "  Voluntary", cpu_diff_stats->voluntary_context_switches);
		printf("\033[10;2H%-14s : %llu", "  Involuntary", cpu_diff_stats->involuntary_context_switches);
		printf("\033[11;2H%-14s : %llu", "Syscalls", cpu_diff_stats->syscalls);
		printf("\033[12;2H%-14s : %llu", "Intrs", cpu_diff_stats->interrupts);
		printf("\033[13;2H%-14s : %llu", "SoftIntrs", cpu_diff_stats->soft_interrupts);
		fflush(stdout);
		ch = inp_wait(delay);
		if( quit || (ch == 'q') )
			break;
	}
	sg_shutdown();

	exit(0);
}
Пример #13
0
int main(int argc, char **argv){

	extern char *optarg;
	int c;

	time_t last_update = 0;

	WINDOW *window;

	extern int errno;

	int delay=2;

	sg_init();
	if(sg_drop_privileges() != 0){
		fprintf(stderr, "Failed to drop setuid/setgid privileges\n");
		return 1;
	}
		
	while ((c = getopt(argc, argv, "vhd:")) != -1){
		switch (c){
			case 'd':
				delay = atoi(optarg);
				if (delay < 1){
					fprintf(stderr, "Time must be 1 second or greater\n");
					exit(1);
				}
				break;
			case 'v':
				version_num(argv[0]);	
				break;
			case 'h':
			default:
				usage(argv[0]);
				return 1;
				break;
		}
	}

	signal(SIGWINCH, sig_winch_handler);
	initscr();
	nonl();
	cbreak();
	noecho();
	timeout(delay * 1000);
	window=newwin(0, 0, 0, 0);
	clear();

	if(!get_stats()){
		fprintf(stderr, "Failed to get all the stats. Please check correct permissions\n");
		endwin();
		return 1;
	}

	display_headings();

	for(;;){
		time_t now;
		int ch = getch();

		if (ch == 'q'){
			break;
		}

		/* To keep the numbers slightly accurate we do not want them
		 * updating more frequently than once a second.
		 */
		now = time(NULL);
		if ((now - last_update) >= 1) {
			get_stats();
		}
		last_update = now;

		display_data();
	}	

	endwin();
	return 0;
}	
Пример #14
0
int main(int argc, char **argv){

	extern char *optarg;
	int c;

	/* We default to 1 second updates and displaying in bytes*/
	int delay = 1;
	char units = 'b';
	unsigned long long divider = 1;

	sg_disk_io_stats *diskio_stats;
	size_t num_diskio_stats;

	/* Parse command line options */
	while ((c = getopt(argc, argv, "d:bkm")) != -1){
		switch (c){
			case 'd':
				delay =	atoi(optarg);
				break;
			case 'b':
				units = 'b';
				break;
			case 'k':
				units = 'k';
				divider = 1024;
				break;
			case 'm':
				units = 'm';
				divider = 1024 * 1024;
				break;
		}
	}

	/* Initialise helper - e.g. logging, if any */
	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

	/* Initialise statgrab */
	sg_init(1);

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

	/* We are not interested in the amount of traffic ever transmitted, just differences.
	 * Because of this, we do nothing for the very first call.
	 */

	register_sig_flagger( SIGINT, &quit );

	diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats);
	if (diskio_stats == NULL)
		sg_die("Failed to get disk stats", 1);

	/* Clear the screen ready for display the disk stats */
	printf("\033[2J");

	/* Keep getting the disk stats */
	while ( (diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats)) != NULL) {
		size_t x;
		int line_number = 2;
		int ch;

		long long total_write=0;
		long long total_read=0;

		qsort(diskio_stats , num_diskio_stats, sizeof(diskio_stats[0]), sg_disk_io_compare_traffic);

		for(x = 0; x < num_diskio_stats; x++){
			/* Print at location 2, linenumber the interface name */
			printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", diskio_stats->disk_name);
			/* Print out at the correct location the traffic in the requsted units passed at command time */
			printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk read", diskio_stats->read_bytes / divider, units);
			printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk write", diskio_stats->write_bytes / divider, units);
			printf("\033[%d;2H%-25s : %ld ", line_number++, "Disk systime", (long) diskio_stats->systime);

			/* Add a blank line between interfaces */
			line_number++;

			/* Add up this interface to the total so we can display a "total" disk io" */
			total_write+=diskio_stats->write_bytes;
			total_read+=diskio_stats->read_bytes;

			/* Move the pointer onto the next interface. Since this returns a static buffer, we dont need
			 * to keep track of the orginal pointer to free later */
			diskio_stats++;
		}

		printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", "Total Disk IO");
		printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk Total read", total_read / divider, units);
		printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk Total write", total_write / divider, units);

		fflush(stdout);

		ch = inp_wait(delay);
		if( quit || (ch == 'q') )
			break;
	}

	return 0;

}
int main(int argc, char **argv){

	extern char *optarg;
	int c;

	/* We default to 1 second updates and displaying in bytes*/
	int delay = 1;
	char units = 'b';
	unsigned long long divider = 1;

	sg_network_io_stats *network_stats;
	size_t num_network_stats;

	/* Parse command line options */
	while ((c = getopt(argc, argv, "d:bkm")) != -1){
		switch (c){
			case 'd':
				delay =	atoi(optarg);
				break;
			case 'b':
				units = 'b';
				break;
			case 'k':
				units = 'k';
				divider = 1024;
				break;
			case 'm':
				units = 'm';
				divider = 1024 * 1024;
				break;
		}
	}

	/* Initialise helper - e.g. logging, if any */
	sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL);

	/* Initialise statgrab */
	sg_init(1);

	register_sig_flagger( SIGINT, &quit );

	/* Drop setuid/setgid privileges. */
	if (sg_drop_privileges() != SG_ERROR_NONE)
		sg_die("Error. Failed to drop privileges", 1);

	/* We are not interested in the amount of traffic ever transmitted, just differences.
	 * Because of this, we do nothing for the very first call.
	 */

	network_stats = sg_get_network_io_stats_diff(&num_network_stats);
	if (network_stats == NULL)
		sg_die("Error. Failed to get network stats", 1);

	/* Clear the screen ready for display the network stats */
	printf("\033[2J");

	/* Keep getting the network stats */
	while ( (network_stats = sg_get_network_io_stats_diff(&num_network_stats)) != NULL){
		size_t x;
		int line_number = 2;
		int ch;

		unsigned long long total_tx=0;
		unsigned long long total_rx=0;
		unsigned long long total_ipackets=0;
		unsigned long long total_opackets=0;
		unsigned long long total_ierrors=0;
		unsigned long long total_oerrors=0;
		unsigned long long total_collisions=0;

		for(x = 0; x < num_network_stats; x++){
			/* Print at location 2, linenumber the interface name */
			printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", network_stats->interface_name);
			/* Print out at the correct location the traffic in the requsted units passed at command time */
			printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Interface Rx", network_stats->rx / divider, units);
			printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Interface Tx", network_stats->tx / divider, units);
			printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface packets in", network_stats->ipackets);
			printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface packets out", network_stats->opackets);
			printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface errors in", network_stats->ierrors);
			printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface errors out", network_stats->oerrors);
			printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface collisions", network_stats->collisions);
			printf("\033[%d;2H%-30s : %ld ", line_number++, "Network Interface systime", (long) network_stats->systime);

			/* Add a blank line between interfaces */
			line_number++;

			/* Add up this interface to the total so we can display a "total" network io" */
			total_tx+=network_stats->tx;
			total_rx+=network_stats->rx;
			total_ipackets+=network_stats->ipackets;
			total_opackets+=network_stats->opackets;
			total_ierrors+=network_stats->ierrors;
			total_oerrors+=network_stats->oerrors;
			total_collisions+=network_stats->collisions;

			/* Move the pointer onto the next interface. Since this returns a static buffer, we dont need
			 * to keep track of the orginal pointer to free later */
			network_stats++;
		}

		printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", "Total Network IO");
		printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Total Rx", total_rx / divider, units);
		printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Total Tx", total_tx / divider, units);
		printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total packets in", total_ipackets);
		printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total packets out", total_opackets);
		printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total errors in", total_ierrors);
		printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total errors out", total_oerrors);
		printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total collisions", total_collisions);

		fflush(stdout);

		ch = inp_wait(delay);
		if( quit || (ch == 'q') )
			break;
	}

	return 0;

}
Пример #16
0
int main(int argc, char **argv){

	extern char *optarg;
	int c;

	/* We default to 1 second updates and displaying in bytes*/
	int delay = 1;
	char units = 'b';

	sg_network_io_stats *network_stats;
	int num_network_stats;

	/* Parse command line options */
	while ((c = getopt(argc, argv, "d:bkm")) != -1){
		switch (c){
			case 'd':
				delay =	atoi(optarg);
				break;
			case 'b':
				units = 'b';
				break;
			case 'k':
				units = 'k';	
				break;
			case 'm':
				units = 'm';
				break;
		}
	}

	/* Initialise statgrab */
	sg_init();

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

	/* We are not interested in the amount of traffic ever transmitted, just differences. 
	 * Because of this, we do nothing for the very first call.
	 */

	network_stats = sg_get_network_io_stats_diff(&num_network_stats);
	if (network_stats == NULL){
		perror("Error. Failed to get network stats");
		return 1;
	}

	/* Clear the screen ready for display the network stats */
	printf("\033[2J");

	/* Keep getting the network stats */
	while ( (network_stats = sg_get_network_io_stats_diff(&num_network_stats)) != NULL){
		int x;
		int line_number = 2;

		long long total_tx=0;
		long long total_rx=0;
		long long total_ipackets=0;
		long long total_opackets=0;
		long long total_ierrors=0;
		long long total_oerrors=0;
		long long total_collisions=0;

		for(x = 0; x < num_network_stats; x++){	
			/* Print at location 2, linenumber the interface name */
			printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", network_stats->interface_name);
			/* Print out at the correct location the traffic in the requsted units passed at command time */
			switch(units){
				case 'b':
					printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Interface Rx", network_stats->rx);
					printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Interface Tx", network_stats->tx);
					break;
				case 'k':
					printf("\033[%d;2H%-30s : %5lld k", line_number++, "Network Interface Rx", (network_stats->rx / 1024));
					printf("\033[%d;2H%-30s : %5lld", line_number++, "Network Interface Tx", (network_stats->tx / 1024));
					break;
				case 'm':
					printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Interface Rx", network_stats->rx / (1024.00*1024.00));
					printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Interface Tx", network_stats->tx / (1024.00*1024.00));
			}
			printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface packets in", network_stats->ipackets);
			printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface packets out", network_stats->opackets);
			printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface errors in", network_stats->ierrors);
			printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface errors out", network_stats->oerrors);
			printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Interface collisions", network_stats->collisions);
			printf("\033[%d;2H%-30s : %ld ", line_number++, "Network Interface systime", (long) network_stats->systime);

			/* Add a blank line between interfaces */	
			line_number++;

			/* Add up this interface to the total so we can display a "total" network io" */
			total_tx+=network_stats->tx;
			total_rx+=network_stats->rx;
			total_ipackets+=network_stats->ipackets;
			total_opackets+=network_stats->opackets;
			total_ierrors+=network_stats->ierrors;
			total_oerrors+=network_stats->oerrors;
			total_collisions+=network_stats->collisions;

			/* Move the pointer onto the next interface. Since this returns a static buffer, we dont need
			 * to keep track of the orginal pointer to free later */
			network_stats++;
		}

		printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", "Total Network IO");
		switch(units){
			case 'b':
				printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Total Rx", total_rx);
				printf("\033[%d;2H%-30s : %8lld b", line_number++, "Network Total Tx", total_tx);
				break;
			case 'k':
				printf("\033[%d;2H%-30s : %5lld k", line_number++, "Network Total Rx", (total_rx / 1024));
				printf("\033[%d;2H%-30s : %5lld k", line_number++, "Network Total Tx", (total_tx / 1024));
				break;
			case 'm':
				printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Total Rx", (total_rx  / (1024.00*1024.00)));
				printf("\033[%d;2H%-30s : %5.2f m", line_number++, "Network Total Tx", (total_tx  / (1024.00*1024.00)));
				break;
		}
		printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total packets in", total_ipackets);
		printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total packets out", total_opackets);
		printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total errors in", total_ierrors);
		printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total errors out", total_oerrors);
		printf("\033[%d;2H%-30s : %lld ", line_number++, "Network Total collisions", total_collisions);

		fflush(stdout);

		sleep(delay);

	}

	return 0;

}