//! Returns only if requested by run_once = TRUE //! Pointer to get updated config int run_stats(int run_once, int execution_interval_sec, int (*update_config_fn)(void)) { LOGDEBUG("Starting stats loop with execution interval %d sec\n", execution_interval_sec); useconds_t start_time_us = 0; useconds_t execution_interval_us = execution_interval_sec * 1000 * 1000; useconds_t sleep_time_us = execution_interval_us; do { LOGTRACE("Sleeping for %d usec for next pass\n", sleep_time_us); usleep(sleep_time_us); start_time_us = time_usec(); //update config if(update_config_fn) { LOGTRACE("Getting updated config\n"); update_config_fn(); LOGTRACE("Done getting updated config\n"); } //Run internal stats sensor updates LOGTRACE("Executing internal stats sensor pass...not quite\n"); //Don't return immediately on failure, run all internal sensors if(internal_sensor_pass(FALSE) != EUCA_OK) { LOGERROR("Error encountered during internal stats sensor run.\n"); } else { LOGTRACE("Internal stats sensor pass completed successfully\n"); } sleep_time_us = execution_interval_us - (time_usec() - start_time_us); } while(!run_once); LOGDEBUG("Returning from run_stats()\n"); return EUCA_OK; }
void cpustats_read(struct cpustats *out) { // On kernels prior to 2.6.37, this can take a long time on // large systems because updating IRQ counts is slow. See // https://lkml.org/lkml/2010/9/29/259 int i; for (i = 0; i < cpustats_cpus; i++) out->cpus[i].online = false; out->online = out->max = 0; out->real = time_usec() * sysconf(_SC_CLK_TCK) / 1000000; if ((readn_str(cpustats_fd, cpustats_buf, cpustats_buf_size)) < 0) epanic("failed to read %s/stat", proc_path); char *pos = cpustats_buf; while (strncmp(pos, "cpu", 3) == 0) { pos += 3; struct cpustat *st; int cpu = -1; if (*pos == ' ') { // Aggregate line st = &out->avg; } else if (isdigit(*pos)) { cpu = strtol(pos, &pos, 10); if (cpu >= cpustats_cpus) goto next; st = &out->cpus[cpu]; } else { goto next; } // Earlier versions of Linux only reported user, nice, // sys, and idle. st->iowait = st->irq = st->softirq = 0; unsigned long long toss; if (sscanf(pos, " %llu %llu %llu %llu %llu %llu %llu", &st->user, &st->nice, &st->sys, &toss, &st->iowait, &st->irq, &st->softirq) < 4) continue; st->online = true; if (cpu != -1) out->online++; if (cpu > out->max) out->max = cpu; next: // Go to the next line while (*pos && *pos != '\n') pos++; if (*pos) pos++; } if ((lseek(cpustats_fd, 0, SEEK_SET)) < 0) epanic("failed to seek %s/stat", proc_path); }
/* Return a well formatted string with a time difference at millisecond resolution */ char * elapsed_time (struct timeval * start, struct timeval * stop) { static char et [64]; time_t elapsed = delta_time_in_milliseconds (stop, start); if (time_day (elapsed)) sprintf (et, "%d days, %02d:%02d:%02d.%03ld", time_day (elapsed), time_hour (elapsed), time_min (elapsed), time_sec (elapsed), time_usec (elapsed)); else if (time_hour (elapsed)) sprintf (et, "%02d:%02d:%02d.%03ld", time_hour (elapsed), time_min (elapsed), time_sec (elapsed), time_usec (elapsed)); else if (time_min (elapsed)) sprintf (et, "%02d:%02d.%03ld", time_min (elapsed), time_sec (elapsed), time_usec (elapsed)); else if (time_sec (elapsed)) sprintf (et, "%d.%03d secs", time_sec (elapsed), time_msec (elapsed)); else sprintf (et, "%3d msecs", time_msec (elapsed)); return et; }
void on_write(int fd, short ev, void *arg) { ServerStats *server = (ServerStats *)arg; int write_len; write_len = write(server->fd_server, test_vars.string_test, test_vars.string_length); /* update statistics */ server->time = time_usec(); server->requests++; server->sent_data += write_len; /* close the socket and delete the event once all data sent */ if (server->requests == test_vars.repeat) { event_del(&server->ev_write); } }
int get_socket(int connection) { struct sockaddr_in addr; struct hostent *he; int sock_buf_size; /* create the socket */ if ((srv_stats[connection].fd_server = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket"); bzero((char *)&addr, sizeof(addr)); /* resolve the hostname */ if ((he = gethostbyname(test_vars.server)) == NULL) err(1, "gethostbyname"); bcopy(he->h_addr, &addr.sin_addr, he->h_length); addr.sin_family = AF_INET; addr.sin_port = htons(test_vars.port); /* connect! */ if (connect(srv_stats[connection].fd_server, (struct sockaddr *) &addr, sizeof(addr)) < 0) { warn("connect"); return ERROR; } /* insert initial values for this connection */ srv_stats[connection].time = time_usec(); srv_stats[connection].local_port = get_local_Port(srv_stats[connection].fd_server); srv_stats[connection].requests = 0; srv_stats[connection].sent_data = 0; srv_stats[connection].recv_data = 0; srv_stats[connection].delay = 0; /* increase the buffer size */ sock_buf_size = MAX_IOSIZE * 2; setsockopt(srv_stats[connection].fd_server, SOL_SOCKET, SO_SNDBUF, &sock_buf_size, sizeof(sock_buf_size)); setsockopt(srv_stats[connection].fd_server, SOL_SOCKET, SO_RCVBUF, &sock_buf_size, sizeof(sock_buf_size)); return srv_stats[connection].fd_server; }
void on_read(int fd, short ev, void *arg) { ServerStats *server = (ServerStats *)arg; u_char buffer[MAX_IOSIZE]; int read_len; read_len = read(server->fd_server, buffer, sizeof(buffer)); /* update statistic */ server->delay += time_usec() - server->time; server->recv_data += read_len; /* close the socket and delete the event once all data received */ if (server->recv_data == (test_vars.repeat * test_vars.string_length)) { event_del(&server->ev_read); close(fd); /* exit the event loop when we have no more connections */ if (--test_vars.open_conns == 0) event_loopexit(0); return; } }
void *proc_main(void *ptr) { (void)ptr; info("PROC Plugin thread created with task id %d", gettid()); if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0) error("Cannot set pthread cancel type to DEFERRED."); if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) error("Cannot set pthread cancel state to ENABLE."); // disable (by default) various interface that are not needed config_get_boolean("plugin:proc:/proc/net/dev:lo", "enabled", 0); config_get_boolean("plugin:proc:/proc/net/dev:fireqos_monitor", "enabled", 0); // when ZERO, attempt to do it int vdo_proc_net_dev = !config_get_boolean("plugin:proc", "/proc/net/dev", 1); int vdo_proc_diskstats = !config_get_boolean("plugin:proc", "/proc/diskstats", 1); int vdo_proc_net_snmp = !config_get_boolean("plugin:proc", "/proc/net/snmp", 1); int vdo_proc_net_snmp6 = !config_get_boolean("plugin:proc", "/proc/net/snmp6", 1); int vdo_proc_net_netstat = !config_get_boolean("plugin:proc", "/proc/net/netstat", 1); int vdo_proc_net_stat_conntrack = !config_get_boolean("plugin:proc", "/proc/net/stat/conntrack", 1); int vdo_proc_net_ip_vs_stats = !config_get_boolean("plugin:proc", "/proc/net/ip_vs/stats", 1); int vdo_proc_net_stat_synproxy = !config_get_boolean("plugin:proc", "/proc/net/stat/synproxy", 1); int vdo_proc_stat = !config_get_boolean("plugin:proc", "/proc/stat", 1); int vdo_proc_meminfo = !config_get_boolean("plugin:proc", "/proc/meminfo", 1); int vdo_proc_vmstat = !config_get_boolean("plugin:proc", "/proc/vmstat", 1); int vdo_proc_net_rpc_nfs = !config_get_boolean("plugin:proc", "/proc/net/rpc/nfs", 1); int vdo_proc_net_rpc_nfsd = !config_get_boolean("plugin:proc", "/proc/net/rpc/nfsd", 1); int vdo_proc_sys_kernel_random_entropy_avail = !config_get_boolean("plugin:proc", "/proc/sys/kernel/random/entropy_avail", 1); int vdo_proc_interrupts = !config_get_boolean("plugin:proc", "/proc/interrupts", 1); int vdo_proc_softirqs = !config_get_boolean("plugin:proc", "/proc/softirqs", 1); int vdo_proc_net_softnet_stat = !config_get_boolean("plugin:proc", "/proc/net/softnet_stat", 1); int vdo_proc_loadavg = !config_get_boolean("plugin:proc", "/proc/loadavg", 1); int vdo_ipc = !config_get_boolean("plugin:proc", "ipc", 1); int vdo_sys_kernel_mm_ksm = !config_get_boolean("plugin:proc", "/sys/kernel/mm/ksm", 1); int vdo_cpu_netdata = !config_get_boolean("plugin:proc", "netdata server resources", 1); // keep track of the time each module was called unsigned long long sutime_proc_net_dev = 0ULL; unsigned long long sutime_proc_diskstats = 0ULL; unsigned long long sutime_proc_net_snmp = 0ULL; unsigned long long sutime_proc_net_snmp6 = 0ULL; unsigned long long sutime_proc_net_netstat = 0ULL; unsigned long long sutime_proc_net_stat_conntrack = 0ULL; unsigned long long sutime_proc_net_ip_vs_stats = 0ULL; unsigned long long sutime_proc_net_stat_synproxy = 0ULL; unsigned long long sutime_proc_stat = 0ULL; unsigned long long sutime_proc_meminfo = 0ULL; unsigned long long sutime_proc_vmstat = 0ULL; unsigned long long sutime_proc_net_rpc_nfs = 0ULL; unsigned long long sutime_proc_net_rpc_nfsd = 0ULL; unsigned long long sutime_proc_sys_kernel_random_entropy_avail = 0ULL; unsigned long long sutime_proc_interrupts = 0ULL; unsigned long long sutime_proc_softirqs = 0ULL; unsigned long long sutime_proc_net_softnet_stat = 0ULL; unsigned long long sutime_proc_loadavg = 0ULL; unsigned long long sutime_ipc = 0ULL; unsigned long long sutime_sys_kernel_mm_ksm = 0ULL; unsigned long long step = rrd_update_every * 1000000ULL; for(;;) { unsigned long long now = time_usec(); unsigned long long next = now - (now % step) + step; while(now < next) { sleep_usec(next - now); now = time_usec(); } if(unlikely(netdata_exit)) break; // BEGIN -- the job to be done if(!vdo_sys_kernel_mm_ksm) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_sys_kernel_mm_ksm()."); now = time_usec(); vdo_sys_kernel_mm_ksm = do_sys_kernel_mm_ksm(rrd_update_every, (sutime_sys_kernel_mm_ksm > 0)?now - sutime_sys_kernel_mm_ksm:0ULL); sutime_sys_kernel_mm_ksm = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_loadavg) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_loadavg()."); now = time_usec(); vdo_proc_loadavg = do_proc_loadavg(rrd_update_every, (sutime_proc_loadavg > 0)?now - sutime_proc_loadavg:0ULL); sutime_proc_loadavg = now; } if(unlikely(netdata_exit)) break; if(!vdo_ipc) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_ipc()."); now = time_usec(); vdo_ipc = do_ipc(rrd_update_every, (sutime_ipc > 0)?now - sutime_ipc:0ULL); sutime_ipc = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_interrupts) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_interrupts()."); now = time_usec(); vdo_proc_interrupts = do_proc_interrupts(rrd_update_every, (sutime_proc_interrupts > 0)?now - sutime_proc_interrupts:0ULL); sutime_proc_interrupts = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_softirqs) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_softirqs()."); now = time_usec(); vdo_proc_softirqs = do_proc_softirqs(rrd_update_every, (sutime_proc_softirqs > 0)?now - sutime_proc_softirqs:0ULL); sutime_proc_softirqs = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_softnet_stat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_softnet_stat()."); now = time_usec(); vdo_proc_net_softnet_stat = do_proc_net_softnet_stat(rrd_update_every, (sutime_proc_net_softnet_stat > 0)?now - sutime_proc_net_softnet_stat:0ULL); sutime_proc_net_softnet_stat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_sys_kernel_random_entropy_avail) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_sys_kernel_random_entropy_avail()."); now = time_usec(); vdo_proc_sys_kernel_random_entropy_avail = do_proc_sys_kernel_random_entropy_avail(rrd_update_every, (sutime_proc_sys_kernel_random_entropy_avail > 0)?now - sutime_proc_sys_kernel_random_entropy_avail:0ULL); sutime_proc_sys_kernel_random_entropy_avail = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_dev) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_dev()."); now = time_usec(); vdo_proc_net_dev = do_proc_net_dev(rrd_update_every, (sutime_proc_net_dev > 0)?now - sutime_proc_net_dev:0ULL); sutime_proc_net_dev = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_diskstats) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_diskstats()."); now = time_usec(); vdo_proc_diskstats = do_proc_diskstats(rrd_update_every, (sutime_proc_diskstats > 0)?now - sutime_proc_diskstats:0ULL); sutime_proc_diskstats = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_snmp) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_snmp()."); now = time_usec(); vdo_proc_net_snmp = do_proc_net_snmp(rrd_update_every, (sutime_proc_net_snmp > 0)?now - sutime_proc_net_snmp:0ULL); sutime_proc_net_snmp = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_snmp6) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_snmp6()."); now = time_usec(); vdo_proc_net_snmp6 = do_proc_net_snmp6(rrd_update_every, (sutime_proc_net_snmp6 > 0)?now - sutime_proc_net_snmp6:0ULL); sutime_proc_net_snmp6 = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_netstat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_netstat()."); now = time_usec(); vdo_proc_net_netstat = do_proc_net_netstat(rrd_update_every, (sutime_proc_net_netstat > 0)?now - sutime_proc_net_netstat:0ULL); sutime_proc_net_netstat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_stat_conntrack) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_stat_conntrack()."); now = time_usec(); vdo_proc_net_stat_conntrack = do_proc_net_stat_conntrack(rrd_update_every, (sutime_proc_net_stat_conntrack > 0)?now - sutime_proc_net_stat_conntrack:0ULL); sutime_proc_net_stat_conntrack = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_ip_vs_stats) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_net_ip_vs_stats()."); now = time_usec(); vdo_proc_net_ip_vs_stats = do_proc_net_ip_vs_stats(rrd_update_every, (sutime_proc_net_ip_vs_stats > 0)?now - sutime_proc_net_ip_vs_stats:0ULL); sutime_proc_net_ip_vs_stats = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_stat_synproxy) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_net_stat_synproxy()."); now = time_usec(); vdo_proc_net_stat_synproxy = do_proc_net_stat_synproxy(rrd_update_every, (sutime_proc_net_stat_synproxy > 0)?now - sutime_proc_net_stat_synproxy:0ULL); sutime_proc_net_stat_synproxy = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_stat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_stat()."); now = time_usec(); vdo_proc_stat = do_proc_stat(rrd_update_every, (sutime_proc_stat > 0)?now - sutime_proc_stat:0ULL); sutime_proc_stat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_meminfo) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_meminfo()."); now = time_usec(); vdo_proc_meminfo = do_proc_meminfo(rrd_update_every, (sutime_proc_meminfo > 0)?now - sutime_proc_meminfo:0ULL); sutime_proc_meminfo = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_vmstat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_vmstat()."); now = time_usec(); vdo_proc_vmstat = do_proc_vmstat(rrd_update_every, (sutime_proc_vmstat > 0)?now - sutime_proc_vmstat:0ULL); sutime_proc_vmstat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_rpc_nfsd) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_rpc_nfsd()."); now = time_usec(); vdo_proc_net_rpc_nfsd = do_proc_net_rpc_nfsd(rrd_update_every, (sutime_proc_net_rpc_nfsd > 0)?now - sutime_proc_net_rpc_nfsd:0ULL); sutime_proc_net_rpc_nfsd = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_rpc_nfs) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_rpc_nfs()."); now = time_usec(); vdo_proc_net_rpc_nfs = do_proc_net_rpc_nfs(rrd_update_every, (sutime_proc_net_rpc_nfs > 0)?now - sutime_proc_net_rpc_nfs:0ULL); sutime_proc_net_rpc_nfs = now; } if(unlikely(netdata_exit)) break; // END -- the job is done // -------------------------------------------------------------------- if(!vdo_cpu_netdata) { global_statistics_charts(); registry_statistics(); } } info("PROC thread exiting"); pthread_exit(NULL); return NULL; }
int main(int argc, char **argv) { struct time_type time; ZOOM_connection *z; ZOOM_resultset *r; int *elc; struct event_line_t *els; ZOOM_options o; int i; int k; init_statics(); read_params(argc, argv, ¶meters); z = (ZOOM_connection *) xmalloc(sizeof(*z) * parameters.concurrent); r = (ZOOM_resultset *) xmalloc(sizeof(*r) * parameters.concurrent); elc = (int *) xmalloc(sizeof(*elc) * parameters.concurrent * parameters.repeat); els = (struct event_line_t *) xmalloc( sizeof(*els) * parameters.concurrent * parameters.repeat * 10); o = ZOOM_options_create(); /* async mode */ ZOOM_options_set (o, "async", "1"); /* get first record of result set (using piggypack) */ if (parameters.piggypack) ZOOM_options_set (o, "count", "1"); /* set proxy */ if (strlen(parameters.proxy)) ZOOM_options_set (o, "proxy", parameters.proxy); /* preferred record syntax */ if (0){ ZOOM_options_set (o, "preferredRecordSyntax", "usmarc"); ZOOM_options_set (o, "elementSetName", "F"); } time_init(&time); /* repeat loop */ for (k = 0; k < parameters.repeat; k++){ /* progress zeroing */ for (i = 0; i < 4096; i++){ parameters.progress[i] = k * 5 -1; } /* connect to all concurrent connections*/ for ( i = 0; i < parameters.concurrent; i++){ /* set event count to zero */ elc[k * parameters.concurrent + i] = 0; /* create connection - pass options (they are the same for all) */ z[i] = ZOOM_connection_create(o); /* connect and init */ ZOOM_connection_connect(z[i], parameters.host, 0); } /* search all */ for (i = 0; i < parameters.concurrent; i++) r[i] = ZOOM_connection_search_pqf (z[i], parameters.query); /* network I/O. pass number of connections and array of connections */ while ((i = ZOOM_event (parameters.concurrent, z))){ int event = ZOOM_connection_last_event(z[i-1]); const char *errmsg; const char *addinfo; int error = 0; //int progress = zoom_progress[event]; if (event == ZOOM_EVENT_SEND_DATA || event == ZOOM_EVENT_RECV_DATA) continue; time_stamp(&time); /* updating events and event list */ error = ZOOM_connection_error(z[i-1] , &errmsg, &addinfo); if (error) parameters.progress[i] = zoom_progress[ZOOM_EVENT_UNKNOWN]; //parameters.progress[i] = zoom_progress[ZOOM_EVENT_NONE]; else if (event == ZOOM_EVENT_CONNECT) parameters.progress[i] = zoom_progress[event]; else //parameters.progress[i] = zoom_progress[event]; parameters.progress[i] += 1; update_events(elc, els, k, i-1, time_sec(&time), time_usec(&time), parameters.progress[i], event, zoom_events[event], error, errmsg); } /* destroy connections */ for (i = 0; i<parameters.concurrent; i++) { ZOOM_resultset_destroy (r[i]); ZOOM_connection_destroy (z[i]); } } /* for (k = 0; k < parameters.repeat; k++) repeat loop */ /* output */ if (parameters.gnuplot){ printf("# gnuplot data and instruction file \n"); printf("# gnuplot thisfile \n"); printf("\n"); printf("set title \"Z39.50 connection plot\"\n"); printf("set xlabel \"Connection\"\n"); printf("set ylabel \"Time Seconds\"\n"); printf("set zlabel \"Progress\"\n"); printf("set ticslevel 0\n"); printf("set grid\n"); printf("set pm3d\n"); printf("splot '-' using ($1):($2):($3) t '' with points\n"); printf("\n"); printf("\n"); } print_table_header(); print_events(elc, els, parameters.concurrent); if (parameters.gnuplot){ printf("end\n"); printf("pause -1 \"Hit ENTER to return\"\n"); } /* destroy data structures and exit */ xfree(z); xfree(r); xfree(elc); xfree(els); ZOOM_options_destroy(o); exit (0); }
int main(void) { char *status; if((status = malloc(STATUS_LENGTH*sizeof(char))) == NULL){ printf("Cannot allocate memory for status"); return 0; } char *datetime = NULL; char state_bat[12]; int bat1, energy_full; // bat2; int rem_hours = 0, rem_min = 0, rem_sec = 0; long rem_usec = 0; char *net_intf, *ip = NULL; if((net_intf = malloc(5*sizeof(char))) == NULL){ printf("Cannot allocate memory for net_intf"); return 0; } if((ip = malloc(100*sizeof(char))) == NULL){ printf("Cannot allocate memory for ip"); return 0; } char net_displ[50]; long left_volume; long right_volume; int mem_used; int mem_available; float cpu_avg_freq; long usec1, usec2 = 0; struct timeval time1, time2; int connected = 0; int energy1, energy2, dummy = 0; int count = 0; char *mpc_vol, *mpc_state, *mpc_song = NULL; if((mpc_vol = malloc(3*sizeof(char))) == NULL){ printf("Cannot allocate memory for mpc_vol"); return 0; } if((mpc_state = malloc(5*sizeof(char))) == NULL){ printf("Cannot allocate memory for mpc_state"); return 0; } if((mpc_song = malloc(100*sizeof(char))) == NULL){ printf("Cannot allocate memory for mpc_song"); return 0; } if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "Cannot open display.\n"); return 0; } strcpy(state_bat, "unknown"); for (;;sleep(1)) { bat1 = getbattery(&dummy, &energy_full); connected = net(&net_intf, &ip); datetime = getdatetime(); getvolume(&left_volume, &right_volume); getmemoryusage(&mem_available, &mem_used); getcpuusage(&cpu_avg_freq); if (connected == 1) sprintf(net_displ, "%s: %s", net_intf, ip); else sprintf(net_displ, "not connected"); //mpc_stat(&mpc_vol, &mpc_state, &mpc_song); if (count == 0) { usec1 = time_usec(&time1); getbattery(&energy1, &energy_full); count++; } else if (count >= 5) { count = 0; usec2 = time_usec(&time2); getbattery(&energy2, &energy_full); if ((energy2 - energy1) < 0){ rem_usec = ((float)(usec2-usec1))*(((float)(energy1+energy2))/((float)(2*(energy1-energy2)))); strcpy(state_bat, "discharging"); } else if ((energy2 - energy1) > 0) { rem_usec = (((float)(usec2-usec1))*((float)energy_full)/((float)(energy2-energy1)))*(1-(((float)(energy2+energy1))/((float)(2*energy_full)))); strcpy(state_bat, "charging"); } else if ( (energy2 == energy1) && bat1 >= 100) { rem_usec = 0; strcpy(state_bat, "charged"); } else { rem_usec = 0; strcpy(state_bat, "unknown"); } rem_hours = (int)(rem_usec/(1e6*60*60)); rem_min = (int)((rem_usec/(1e6*60)) - rem_hours*60); rem_sec = (int)((rem_usec/(1e6)) - rem_hours*60*60 - rem_min*60); if ((bat1 < 15) && (strcmp(state_bat, "discharging") == 0)) { system("mplayer /home/andrea/.local/statusbar/sound/siren.mp3"); } } else { count++; } // snprintf(status, STATUS_LENGTH, "MPC: status: %s - song: %s | %s | volume left %i - right %i | Battery %d%% (%s %d h %d min %d s) | %s", mpc_state, mpc_song, net_displ, (int)left_volume, (int)right_volume, bat1, state_bat, rem_hours, rem_min, rem_sec, datetime); snprintf(status, STATUS_LENGTH, "CPU (avg) %4.3f MHz | RAM used %d%% available %d%% | %s | volume left %i - right %i | Battery %d%% (%s %d h %d min %d s) | %s", cpu_avg_freq, mem_used, mem_available, net_displ, (int)left_volume, (int)right_volume, bat1, state_bat, rem_hours, rem_min, rem_sec, datetime); setstatus(status); } free(mpc_song); free(mpc_state); free(mpc_vol); free(net_intf); free(ip); free(datetime); free(status); XCloseDisplay(dpy); return 0; }