2013-08-16 10:55:55 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/sysinfo.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2013-08-16 10:55:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2013-10-08 15:15:08 +00:00
|
|
|
#include <utmpx.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2014-07-01 14:48:35 +00:00
|
|
|
#include "config.h"
|
2013-08-16 10:55:55 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-10-08 15:15:08 +00:00
|
|
|
struct utmpx utx;
|
2013-08-16 10:55:55 +00:00
|
|
|
FILE *ufp;
|
|
|
|
struct sysinfo info;
|
|
|
|
time_t tmptime;
|
|
|
|
struct tm *now;
|
|
|
|
unsigned int days, hours, minutes;
|
|
|
|
int nusers = 0;
|
2013-10-08 15:15:08 +00:00
|
|
|
size_t n;
|
2013-08-16 10:55:55 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2013-08-16 14:51:37 +00:00
|
|
|
if (sysinfo(&info) < 0)
|
|
|
|
eprintf("sysinfo:");
|
2013-08-16 10:55:55 +00:00
|
|
|
time(&tmptime);
|
|
|
|
now = localtime(&tmptime);
|
|
|
|
printf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec);
|
|
|
|
info.uptime /= 60;
|
|
|
|
minutes = info.uptime % 60;
|
|
|
|
info.uptime /= 60;
|
|
|
|
hours = info.uptime % 24;
|
|
|
|
days = info.uptime / 24;
|
|
|
|
if (days)
|
2018-12-28 12:49:54 +00:00
|
|
|
printf("%d day%s, ", days, days != 1 ? "s" : "");
|
2013-08-16 10:55:55 +00:00
|
|
|
if (hours)
|
|
|
|
printf("%2d:%02d, ", hours, minutes);
|
|
|
|
else
|
|
|
|
printf("%d min, ", minutes);
|
|
|
|
|
2014-07-01 14:48:35 +00:00
|
|
|
if ((ufp = fopen(UTMP_PATH, "r"))) {
|
2013-10-08 15:15:08 +00:00
|
|
|
while ((n = fread(&utx, sizeof(utx), 1, ufp)) > 0) {
|
|
|
|
if (!utx.ut_user[0])
|
2013-08-16 10:55:55 +00:00
|
|
|
continue;
|
2013-10-08 15:15:08 +00:00
|
|
|
if (utx.ut_type != USER_PROCESS)
|
2013-09-01 16:50:03 +00:00
|
|
|
continue;
|
2013-08-16 10:55:55 +00:00
|
|
|
nusers++;
|
|
|
|
}
|
2013-10-08 15:15:08 +00:00
|
|
|
if (ferror(ufp))
|
2015-07-09 20:25:33 +00:00
|
|
|
eprintf("%s: read error:", UTMP_PATH);
|
2013-08-16 10:55:55 +00:00
|
|
|
fclose(ufp);
|
2018-12-28 12:49:54 +00:00
|
|
|
printf(" %d user%s, ", nusers, nusers != 1 ? "s" : "");
|
2013-08-16 10:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(" load average: %.02f, %.02f, %.02f\n",
|
|
|
|
info.loads[0] / 65536.0f,
|
|
|
|
info.loads[1] / 65536.0f,
|
|
|
|
info.loads[2] / 65536.0f);
|
|
|
|
|
2014-10-02 22:45:25 +00:00
|
|
|
return 0;
|
2013-08-16 10:55:55 +00:00
|
|
|
}
|