introduce 'core show sysinfo' for systems that dont have the Linux-ish sysinfo stuff but do have sysctl.

(closes issue #13433)
Reported by: mvanbaak
Patches:
      2008121300_sysinfosysctl.diff.txt uploaded by mvanbaak (license 7)
	  with two free calls replaced with ast_free based on feedback on reviewboard
Review:
      http://reviewboard.digium.com/r/91/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@164802 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.2
Michiel van Baak 17 years ago
parent 9de00f16f6
commit d2d96b10ac

2
configure vendored

@ -15675,7 +15675,7 @@ done
for ac_func in asprintf atexit dup2 endpwent ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday ioperm inet_ntoa isascii localtime_r memchr memmove memset mkdir munmap putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtol strtoq unsetenv utime vasprintf getpeereid
for ac_func in asprintf atexit dup2 endpwent ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday ioperm inet_ntoa isascii localtime_r memchr memmove memset mkdir munmap putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtol strtoq sysctl swapctl unsetenv utime vasprintf getpeereid
do
as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
{ echo "$as_me:$LINENO: checking for $ac_func" >&5

@ -337,7 +337,7 @@ AC_FUNC_STRNLEN
AC_FUNC_STRTOD
AC_FUNC_UTIME_NULL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([asprintf atexit dup2 endpwent ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday ioperm inet_ntoa isascii localtime_r memchr memmove memset mkdir munmap putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtol strtoq unsetenv utime vasprintf getpeereid])
AC_CHECK_FUNCS([asprintf atexit dup2 endpwent ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday ioperm inet_ntoa isascii localtime_r memchr memmove memset mkdir munmap putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtol strtoq unsetenv utime vasprintf getpeereid sysctl swapctl])
AC_CHECK_FUNCS([glob])

@ -934,6 +934,12 @@
/* Define to indicate the ${SUPPSERV_DESCRIP} library version */
#undef HAVE_SUPPSERV_VERSION
/* Define to 1 if you have the `swapctl' function. */
#undef HAVE_SWAPCTL
/* Define to 1 if you have the `sysctl' function. */
#undef HAVE_SYSCTL
/* Define to 1 if your system has sysinfo support */
#undef HAVE_SYSINFO

@ -78,6 +78,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <sys/stat.h>
#if defined(HAVE_SYSINFO)
#include <sys/sysinfo.h>
#elif defined(HAVE_SYSCTL)
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/swap.h>
#endif
#include <regex.h>
@ -497,11 +501,114 @@ static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cl
return CLI_SUCCESS;
}
#if defined(HAVE_SYSINFO)
#if defined (HAVE_SYSCTL) && defined(HAVE_SWAPCTL)
/*
* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
* to be based on the new swapctl(2) system call.
*/
static int swapmode(int *used, int *total)
{
struct swapent *swdev;
int nswap, rnswap, i;
nswap = swapctl(SWAP_NSWAP, 0, 0);
if (nswap == 0)
return 0;
swdev = ast_calloc(nswap, sizeof(*swdev));
if (swdev == NULL)
return 0;
rnswap = swapctl(SWAP_STATS, swdev, nswap);
if (rnswap == -1) {
ast_free(swdev);
return 0;
}
/* if rnswap != nswap, then what? */
/* Total things up */
*total = *used = 0;
for (i = 0; i < nswap; i++) {
if (swdev[i].se_flags & SWF_ENABLE) {
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
}
}
ast_free(swdev);
return 1;
}
#elif defined(HAVE_SYSCTL)
static int swapmode(int *used, int *total)
{
used = total = 0;
return 1;
}
#endif
/*! \brief Give an overview of system statistics */
static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
int64_t physmem, freeram;
int totalswap, freeswap, nprocs;
long uptime = 0;
#if defined(HAVE_SYSINFO)
struct sysinfo sys_info;
sysinfo(&sys_info)
uptime = sys_info.uptime/3600;
physmem = sys_info.totalram * sys_info.mem_unit;
freeram = (sys_info.freeram * sys_info.mem_unit) / 1024;
totalswap = (sys_info.totalswap * sys_info.mem_unit) / 1024;
freeswap = (sys_info.freeswap * sys_info.mem_unit) / 1024;
nprocs = sys_info.nprocs;
#elif defined(HAVE_SYSCTL)
static int pageshift;
struct vmtotal vmtotal;
struct timeval boottime;
time_t now;
int mib[2], pagesize, usedswap;
size_t len;
/* calculate the uptime by looking at boottime */
time(&now);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
len = sizeof(boottime);
if (sysctl(mib, 2, &boottime, &len, NULL, 0) != -1) {
uptime = now - boottime.tv_sec;
}
uptime = uptime/3600;
/* grab total physical memory */
mib[0] = CTL_HW;
mib[1] = HW_PHYSMEM64;
len = sizeof(physmem);
sysctl(mib, 2, &physmem, &len, NULL, 0);
pagesize = getpagesize();
pageshift = 0;
while (pagesize > 1) {
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
/* grab vm totals */
mib[0] = CTL_VM;
mib[1] = VM_METER;
len = sizeof(vmtotal);
sysctl(mib, 2, &vmtotal, &len, NULL, 0);
freeram = (vmtotal.t_free << pageshift);
/* generate swap usage and totals */
swapmode(&usedswap, &totalswap);
freeswap = (totalswap - usedswap);
/* grab number of processes */
mib[0] = CTL_KERN;
mib[1] = KERN_NPROCS;
len = sizeof(nprocs);
sysctl(mib, 2, &nprocs, &len, NULL, 0);
#endif
switch (cmd) {
case CLI_INIT:
e->command = "core show sysinfo";
@ -512,22 +619,20 @@ static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cl
case CLI_GENERATE:
return NULL;
}
if (sysinfo(&sys_info)) {
ast_cli(a->fd, "FAILED to retrieve system information\n\n");
return CLI_FAILURE;
}
ast_cli(a->fd, "\nSystem Statistics\n");
ast_cli(a->fd, "-----------------\n");
ast_cli(a->fd, " System Uptime: %ld hours\n", sys_info.uptime/3600);
ast_cli(a->fd, " Total RAM: %ld KiB\n", (sys_info.totalram * sys_info.mem_unit)/1024);
ast_cli(a->fd, " Free RAM: %ld KiB\n", (sys_info.freeram * sys_info.mem_unit)/1024);
ast_cli(a->fd, " System Uptime: %ld hours\n", uptime);
ast_cli(a->fd, " Total RAM: %ld KiB\n", (long)physmem/1024);
ast_cli(a->fd, " Free RAM: %ld KiB\n", (long)freeram);
#if defined(HAVE_SYSINFO)
ast_cli(a->fd, " Buffer RAM: %ld KiB\n", (sys_info.bufferram * sys_info.mem_unit)/1024);
ast_cli(a->fd, " Total Swap Space: %ld KiB\n", (sys_info.totalswap * sys_info.mem_unit)/1024);
ast_cli(a->fd, " Free Swap Space: %ld KiB\n\n", (sys_info.freeswap * sys_info.mem_unit)/1024);
ast_cli(a->fd, " Number of Processes: %d \n\n", sys_info.procs);
#endif
ast_cli(a->fd, " Total Swap Space: %ld KiB\n", (long)totalswap);
ast_cli(a->fd, " Free Swap Space: %ld KiB\n\n", (long)freeswap);
ast_cli(a->fd, " Number of Processes: %d \n\n", nprocs);
return CLI_SUCCESS;
}
#endif
struct profile_entry {
const char *name;
@ -1913,7 +2018,7 @@ static struct ast_cli_entry cli_asterisk[] = {
#if !defined(LOW_MEMORY)
AST_CLI_DEFINE(handle_show_version_files, "List versions of files used to build Asterisk"),
AST_CLI_DEFINE(handle_show_threads, "Show running threads"),
#if defined(HAVE_SYSINFO)
#if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
AST_CLI_DEFINE(handle_show_sysinfo, "Show System Information"),
#endif
AST_CLI_DEFINE(handle_show_profile, "Display profiling info"),

Loading…
Cancel
Save