Asterisk - The Open Source Telephony Project  18.5.0
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  <synopsis>
44  Returns system information specified by parameter.
45  </synopsis>
46  <syntax>
47  <parameter name="parameter" required="true">
48  <enumlist>
49  <enum name="loadavg">
50  <para>System load average from past minute.</para>
51  </enum>
52  <enum name="numcalls">
53  <para>Number of active calls currently in progress.</para>
54  </enum>
55  <enum name="uptime">
56  <para>System uptime in hours.</para>
57  <note><para>This parameter is dependant upon operating system.</para></note>
58  </enum>
59  <enum name="totalram">
60  <para>Total usable main memory size in KiB.</para>
61  <note><para>This parameter is dependant upon operating system.</para></note>
62  </enum>
63  <enum name="freeram">
64  <para>Available memory size in KiB.</para>
65  <note><para>This parameter is dependant upon operating system.</para></note>
66  </enum>
67  <enum name="bufferram">
68  <para>Memory used by buffers in KiB.</para>
69  <note><para>This parameter is dependant upon operating system.</para></note>
70  </enum>
71  <enum name="totalswap">
72  <para>Total swap space still available in KiB.</para>
73  <note><para>This parameter is dependant upon operating system.</para></note>
74  </enum>
75  <enum name="freeswap">
76  <para>Free swap space still available in KiB.</para>
77  <note><para>This parameter is dependant upon operating system.</para></note>
78  </enum>
79  <enum name="numprocs">
80  <para>Number of current processes.</para>
81  <note><para>This parameter is dependant upon operating system.</para></note>
82  </enum>
83  </enumlist>
84  </parameter>
85  </syntax>
86  <description>
87  <para>Returns information from a given parameter.</para>
88  </description>
89  </function>
90  ***/
91 
92 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
93  char *buf, size_t len)
94 {
95 #if defined(HAVE_SYSINFO)
96  struct sysinfo sys_info;
97  if (sysinfo(&sys_info)) {
98  ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
99  return -1;
100  }
101 #endif
102  if (ast_strlen_zero(data)) {
103  ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
104  return -1;
105  } else if (!strcasecmp("loadavg", data)) {
106  double curloadavg;
107  getloadavg(&curloadavg, 1);
108  snprintf(buf, len, "%f", curloadavg);
109  } else if (!strcasecmp("numcalls", data)) {
110  snprintf(buf, len, "%d", ast_active_calls());
111  }
112 #if defined(HAVE_SYSINFO)
113  else if (!strcasecmp("uptime", data)) { /* in hours */
114  snprintf(buf, len, "%ld", sys_info.uptime/3600);
115  } else if (!strcasecmp("totalram", data)) { /* in KiB */
116  snprintf(buf, len, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024);
117  } else if (!strcasecmp("freeram", data)) { /* in KiB */
118  snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024);
119  } else if (!strcasecmp("bufferram", data)) { /* in KiB */
120  snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024);
121  } else if (!strcasecmp("totalswap", data)) { /* in KiB */
122  snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024);
123  } else if (!strcasecmp("freeswap", data)) { /* in KiB */
124  snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024);
125  } else if (!strcasecmp("numprocs", data)) {
126  snprintf(buf, len, "%d", sys_info.procs);
127  }
128 #endif
129  else {
130  ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
131  return -1;
132  }
133 
134  return 0;
135 }
136 
138  .name = "SYSINFO",
139  .read = sysinfo_helper,
140  .read_max = 22,
141 };
142 
143 static int unload_module(void)
144 {
145  return ast_custom_function_unregister(&sysinfo_function);
146 }
147 
148 static int load_module(void)
149 {
150  return ast_custom_function_register(&sysinfo_function);
151 }
152 
153 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
const char * name
Definition: pbx.h:119
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
int ast_active_calls(void)
Retrieve the number of active calls.
Definition: pbx.c:4764
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define ast_strlen_zero(foo)
Definition: strings.h:52
static int unload_module(void)
Definition: func_sysinfo.c:143
static int load_module(void)
Definition: func_sysinfo.c:148
#define ast_log
Definition: astobj2.c:42
static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_sysinfo.c:92
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Core PBX routines and definitions.
#define LOG_ERROR
Definition: logger.h:285
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int getloadavg(double *list, int nelem)
static struct ast_custom_function sysinfo_function
Definition: func_sysinfo.c:137
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508