2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * SYSINFO function to return various system data.
21 * \note Inspiration and Guidance from Russell
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #if defined(HAVE_SYSINFO)
37 #include <sys/sysinfo.h>
40 #include "asterisk/module.h"
41 #include "asterisk/pbx.h"
44 <function name="SYSINFO" language="en_US">
46 Returns system information specified by parameter.
49 <parameter name="parameter" required="true">
52 <para>System load average from past minute.</para>
54 <enum name="numcalls">
55 <para>Number of active calls currently in progress.</para>
58 <para>System uptime in hours.</para>
59 <note><para>This parameter is dependant upon operating system.</para></note>
61 <enum name="totalram">
62 <para>Total usable main memory size in KiB.</para>
63 <note><para>This parameter is dependant upon operating system.</para></note>
66 <para>Available memory size in KiB.</para>
67 <note><para>This parameter is dependant upon operating system.</para></note>
69 <enum name="bufferram">
70 <para>Memory used by buffers in KiB.</para>
71 <note><para>This parameter is dependant upon operating system.</para></note>
73 <enum name="totalswap">
74 <para>Total swap space still available in KiB.</para>
75 <note><para>This parameter is dependant upon operating system.</para></note>
77 <enum name="freeswap">
78 <para>Free swap space still available in KiB.</para>
79 <note><para>This parameter is dependant upon operating system.</para></note>
81 <enum name="numprocs">
82 <para>Number of current processes.</para>
83 <note><para>This parameter is dependant upon operating system.</para></note>
89 <para>Returns information from a given parameter.</para>
94 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
95 char *buf, size_t len)
97 #if defined(HAVE_SYSINFO)
98 struct sysinfo sys_info;
99 if (sysinfo(&sys_info)) {
100 ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
104 if (ast_strlen_zero(data)) {
105 ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
107 } else if (!strcasecmp("loadavg", data)) {
109 getloadavg(&curloadavg, 1);
110 snprintf(buf, len, "%f", curloadavg);
111 } else if (!strcasecmp("numcalls", data)) {
112 snprintf(buf, len, "%d", ast_active_calls());
114 #if defined(HAVE_SYSINFO)
115 else if (!strcasecmp("uptime", data)) { /* in hours */
116 snprintf(buf, len, "%ld", sys_info.uptime/3600);
117 } else if (!strcasecmp("totalram", data)) { /* in KiB */
118 snprintf(buf, len, "%ld",(sys_info.totalram * sys_info.mem_unit)/1024);
119 } else if (!strcasecmp("freeram", data)) { /* in KiB */
120 snprintf(buf, len, "%ld",(sys_info.freeram * sys_info.mem_unit)/1024);
121 } else if (!strcasecmp("bufferram", data)) { /* in KiB */
122 snprintf(buf, len, "%ld",(sys_info.bufferram * sys_info.mem_unit)/1024);
123 } else if (!strcasecmp("totalswap", data)) { /* in KiB */
124 snprintf(buf, len, "%ld",(sys_info.totalswap * sys_info.mem_unit)/1024);
125 } else if (!strcasecmp("freeswap", data)) { /* in KiB */
126 snprintf(buf, len, "%ld",(sys_info.freeswap * sys_info.mem_unit)/1024);
127 } else if (!strcasecmp("numprocs", data)) {
128 snprintf(buf, len, "%d", sys_info.procs);
132 ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
139 static struct ast_custom_function sysinfo_function = {
141 .read = sysinfo_helper,
145 static int unload_module(void)
147 return ast_custom_function_unregister(&sysinfo_function);
150 static int load_module(void)
152 return ast_custom_function_register(&sysinfo_function);
155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");