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 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413589 $")
00035 
00036 #if defined(HAVE_SYSINFO)
00037 #include <sys/sysinfo.h>
00038 #endif
00039 
00040 #include "asterisk/module.h"
00041 #include "asterisk/pbx.h"
00042 
00043 /*** DOCUMENTATION
00044    <function name="SYSINFO" language="en_US">
00045       <synopsis>
00046          Returns system information specified by parameter.
00047       </synopsis>
00048       <syntax>
00049          <parameter name="parameter" required="true">
00050             <enumlist>
00051                <enum name="loadavg">
00052                   <para>System load average from past minute.</para>
00053                </enum>
00054                <enum name="numcalls">
00055                   <para>Number of active calls currently in progress.</para>
00056                </enum>
00057                <enum name="uptime">
00058                   <para>System uptime in hours.</para>
00059                   <note><para>This parameter is dependant upon operating system.</para></note>
00060                </enum>
00061                <enum name="totalram">
00062                   <para>Total usable main memory size in KiB.</para>
00063                   <note><para>This parameter is dependant upon operating system.</para></note>
00064                </enum>
00065                <enum name="freeram">
00066                   <para>Available memory size in KiB.</para>
00067                   <note><para>This parameter is dependant upon operating system.</para></note>
00068                </enum>
00069                <enum name="bufferram">
00070                   <para>Memory used by buffers in KiB.</para>
00071                   <note><para>This parameter is dependant upon operating system.</para></note>
00072                </enum>
00073                <enum name="totalswap">
00074                   <para>Total swap space still available in KiB.</para>
00075                   <note><para>This parameter is dependant upon operating system.</para></note>
00076                </enum>
00077                <enum name="freeswap">
00078                   <para>Free swap space still available in KiB.</para>
00079                   <note><para>This parameter is dependant upon operating system.</para></note>
00080                </enum>
00081                <enum name="numprocs">
00082                   <para>Number of current processes.</para>
00083                   <note><para>This parameter is dependant upon operating system.</para></note>
00084                </enum>
00085             </enumlist>
00086          </parameter>
00087       </syntax>
00088       <description>
00089          <para>Returns information from a given parameter.</para>
00090       </description>
00091    </function>
00092  ***/
00093 
00094 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
00095                                char *buf, size_t len)
00096 {
00097 #if defined(HAVE_SYSINFO)
00098    struct sysinfo sys_info;
00099    if (sysinfo(&sys_info)) {
00100       ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
00101       return -1;
00102    }
00103 #endif
00104    if (ast_strlen_zero(data)) {
00105       ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
00106       return -1;
00107    } else if (!strcasecmp("loadavg", data)) {
00108       double curloadavg;
00109       getloadavg(&curloadavg, 1);
00110       snprintf(buf, len, "%f", curloadavg);
00111    } else if (!strcasecmp("numcalls", data)) {
00112       snprintf(buf, len, "%d", ast_active_calls());
00113    }
00114 #if defined(HAVE_SYSINFO)
00115    else if (!strcasecmp("uptime", data)) {             /* in hours */
00116       snprintf(buf, len, "%ld", sys_info.uptime/3600);
00117    } else if (!strcasecmp("totalram", data)) {         /* in KiB */
00118       snprintf(buf, len, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024);
00119    } else if (!strcasecmp("freeram", data)) {          /* in KiB */
00120       snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024);
00121    } else if (!strcasecmp("bufferram", data)) {        /* in KiB */
00122       snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024);
00123    } else if (!strcasecmp("totalswap", data)) {        /* in KiB */
00124       snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024);
00125    } else if (!strcasecmp("freeswap", data)) {         /* in KiB */
00126       snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024);
00127    } else if (!strcasecmp("numprocs", data)) {
00128       snprintf(buf, len, "%d", sys_info.procs);
00129    }
00130 #endif
00131    else {
00132       ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
00133       return -1;
00134    }
00135       
00136    return 0;
00137 }
00138 
00139 static struct ast_custom_function sysinfo_function = {
00140    .name = "SYSINFO",
00141    .read = sysinfo_helper,
00142    .read_max = 22,
00143 };
00144 
00145 static int unload_module(void)
00146 {
00147    return ast_custom_function_unregister(&sysinfo_function);
00148 }
00149 
00150 static int load_module(void)
00151 {
00152    return ast_custom_function_register(&sysinfo_function);
00153 }
00154 
00155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
00156 

Generated on Thu Apr 16 06:27:36 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6