Thu Apr 28 2011 17:13:33

Asterisk developer's documentation


func_sysinfo.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * SYSINFO function to return various system data.
00020  * 
00021  * \note Inspiration and Guidance from Russell
00022  *
00023  * \author Jeff Peeler
00024  *
00025  * \ingroup functions
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 87233 $")
00031 
00032 #if defined(HAVE_SYSINFO)
00033 #include <sys/sysinfo.h>
00034 #endif
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/pbx.h"
00038 
00039 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
00040                                char *buf, size_t len)
00041 {
00042 #if defined(HAVE_SYSINFO)
00043    struct sysinfo sys_info;
00044    if (sysinfo(&sys_info)) {
00045       ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
00046       return -1;
00047    }
00048 #endif
00049    if (ast_strlen_zero(data)) {
00050       ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
00051       return -1;
00052    } else if (!strcasecmp("loadavg", data)) {
00053       double curloadavg;
00054       getloadavg(&curloadavg, 1);
00055       snprintf(buf, len, "%f", curloadavg);
00056    } else if (!strcasecmp("numcalls", data)) {
00057       snprintf(buf, len, "%d", ast_active_calls());
00058    }
00059 #if defined(HAVE_SYSINFO)
00060    else if (!strcasecmp("uptime", data)) {             /* in hours */
00061       snprintf(buf, len, "%ld", sys_info.uptime/3600);
00062    } else if (!strcasecmp("totalram", data)) {         /* in KiB */
00063       snprintf(buf, len, "%ld",(sys_info.totalram * sys_info.mem_unit)/1024);
00064    } else if (!strcasecmp("freeram", data)) {          /* in KiB */
00065       snprintf(buf, len, "%ld",(sys_info.freeram * sys_info.mem_unit)/1024);
00066    } else if (!strcasecmp("bufferram", data)) {        /* in KiB */
00067       snprintf(buf, len, "%ld",(sys_info.bufferram * sys_info.mem_unit)/1024);
00068    } else if (!strcasecmp("totalswap", data)) {        /* in KiB */
00069       snprintf(buf, len, "%ld",(sys_info.totalswap * sys_info.mem_unit)/1024);
00070    } else if (!strcasecmp("freeswap", data)) {         /* in KiB */
00071       snprintf(buf, len, "%ld",(sys_info.freeswap * sys_info.mem_unit)/1024);
00072    } else if (!strcasecmp("numprocs", data)) {
00073       snprintf(buf, len, "%d", sys_info.procs);
00074    }
00075 #endif
00076    else {
00077       ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
00078       return -1;
00079    }
00080       
00081    return 0;
00082 }
00083 
00084 static struct ast_custom_function sysinfo_function = {
00085    .name = "SYSINFO",
00086    .synopsis = "Returns system information specified by parameter.",
00087    .syntax = "SYSINFO(<parameter>)",
00088    .read = sysinfo_helper,
00089    .desc = 
00090 "Returns information from a given parameter\n"
00091 "  Options:\n"
00092 "    loadavg   - system load average from past minute\n"
00093 "    numcalls  - number of active calls currently in progress\n"
00094 #if defined(HAVE_SYSINFO)
00095 "    uptime    - system uptime in hours\n"
00096 "    totalram  - total usable main memory size in KiB\n"
00097 "    freeram   - available memory size in KiB\n"
00098 "    bufferram - memory used by buffers in KiB\n"
00099 "    totalswap - total swap space size in KiB\n"
00100 "    freeswap  - free swap space still available in KiB\n"
00101 "    numprocs  - number of current processes\n",
00102 #endif /* HAVE_SYSINFO */
00103 };
00104 
00105 static int unload_module(void)
00106 {
00107    return ast_custom_function_unregister(&sysinfo_function);
00108 }
00109 
00110 static int load_module(void)
00111 {
00112    return ast_custom_function_register(&sysinfo_function);
00113 }
00114 
00115 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
00116