Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_sysinfo.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007, Digium, Inc.
5 *
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.
11 *
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.
15 */
16
17/*! \file
18 *
19 * SYSINFO function to return various system data.
20 *
21 * \note Inspiration and Guidance from Russell
22 *
23 * \author Jeff Peeler
24 *
25 * \ingroup functions
26 */
27
28/*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32#include "asterisk.h"
33
34#if defined(HAVE_SYSINFO)
35#include <sys/sysinfo.h>
36#endif
37
38#include "asterisk/module.h"
39#include "asterisk/pbx.h"
40
41/*** DOCUMENTATION
42 <function name="SYSINFO" language="en_US">
43 <since>
44 <version>1.6.0</version>
45 </since>
46 <synopsis>
47 Returns system information specified by parameter.
48 </synopsis>
49 <syntax>
50 <parameter name="parameter" required="true">
51 <enumlist>
52 <enum name="loadavg">
53 <para>System load average from past minute.</para>
54 </enum>
55 <enum name="numcalls">
56 <para>Number of active calls currently in progress.</para>
57 </enum>
58 <enum name="uptime">
59 <para>System uptime in hours.</para>
60 <note><para>This parameter is dependant upon operating system.</para></note>
61 </enum>
62 <enum name="totalram">
63 <para>Total usable main memory size in KiB.</para>
64 <note><para>This parameter is dependant upon operating system.</para></note>
65 </enum>
66 <enum name="freeram">
67 <para>Available memory size in KiB.</para>
68 <note><para>This parameter is dependant upon operating system.</para></note>
69 </enum>
70 <enum name="bufferram">
71 <para>Memory used by buffers in KiB.</para>
72 <note><para>This parameter is dependant upon operating system.</para></note>
73 </enum>
74 <enum name="totalswap">
75 <para>Total swap space still available in KiB.</para>
76 <note><para>This parameter is dependant upon operating system.</para></note>
77 </enum>
78 <enum name="freeswap">
79 <para>Free swap space still available in KiB.</para>
80 <note><para>This parameter is dependant upon operating system.</para></note>
81 </enum>
82 <enum name="numprocs">
83 <para>Number of current processes.</para>
84 <note><para>This parameter is dependant upon operating system.</para></note>
85 </enum>
86 </enumlist>
87 </parameter>
88 </syntax>
89 <description>
90 <para>Returns information from a given parameter.</para>
91 </description>
92 </function>
93 ***/
94
95static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
96 char *buf, size_t len)
97{
98#if defined(HAVE_SYSINFO)
99 struct sysinfo sys_info;
100 if (sysinfo(&sys_info)) {
101 ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
102 return -1;
103 }
104#endif
105 if (ast_strlen_zero(data)) {
106 ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
107 return -1;
108 } else if (!strcasecmp("loadavg", data)) {
109 double curloadavg;
110 getloadavg(&curloadavg, 1);
111 snprintf(buf, len, "%f", curloadavg);
112 } else if (!strcasecmp("numcalls", data)) {
113 snprintf(buf, len, "%d", ast_active_calls());
114 }
115#if defined(HAVE_SYSINFO)
116 else if (!strcasecmp("uptime", data)) { /* in hours */
117 snprintf(buf, len, "%ld", sys_info.uptime/3600);
118 } else if (!strcasecmp("totalram", data)) { /* in KiB */
119 snprintf(buf, len, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024);
120 } else if (!strcasecmp("freeram", data)) { /* in KiB */
121 snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024);
122 } else if (!strcasecmp("bufferram", data)) { /* in KiB */
123 snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024);
124 } else if (!strcasecmp("totalswap", data)) { /* in KiB */
125 snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024);
126 } else if (!strcasecmp("freeswap", data)) { /* in KiB */
127 snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024);
128 } else if (!strcasecmp("numprocs", data)) {
129 snprintf(buf, len, "%d", sys_info.procs);
130 }
131#endif
132 else {
133 ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
134 return -1;
135 }
136
137 return 0;
138}
139
141 .name = "SYSINFO",
142 .read = sysinfo_helper,
143 .read_max = 22,
144};
145
146static int unload_module(void)
147{
149}
150
151static int load_module(void)
152{
154}
155
156AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
int getloadavg(double *list, int nelem)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_sysinfo.c:95
static int load_module(void)
Definition: func_sysinfo.c:151
static int unload_module(void)
Definition: func_sysinfo.c:146
static struct ast_custom_function sysinfo_function
Definition: func_sysinfo.c:140
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
int ast_active_calls(void)
Retrieve the number of active calls.
Definition: pbx.c:4775
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119