Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
35 
36 #if defined(HAVE_SYSINFO)
37 #include <sys/sysinfo.h>
38 #endif
39 
40 #include "asterisk/module.h"
41 #include "asterisk/pbx.h"
42 
43 /*** DOCUMENTATION
44  <function name="SYSINFO" language="en_US">
45  <synopsis>
46  Returns system information specified by parameter.
47  </synopsis>
48  <syntax>
49  <parameter name="parameter" required="true">
50  <enumlist>
51  <enum name="loadavg">
52  <para>System load average from past minute.</para>
53  </enum>
54  <enum name="numcalls">
55  <para>Number of active calls currently in progress.</para>
56  </enum>
57  <enum name="uptime">
58  <para>System uptime in hours.</para>
59  <note><para>This parameter is dependant upon operating system.</para></note>
60  </enum>
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>
64  </enum>
65  <enum name="freeram">
66  <para>Available memory size in KiB.</para>
67  <note><para>This parameter is dependant upon operating system.</para></note>
68  </enum>
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>
72  </enum>
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>
76  </enum>
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>
80  </enum>
81  <enum name="numprocs">
82  <para>Number of current processes.</para>
83  <note><para>This parameter is dependant upon operating system.</para></note>
84  </enum>
85  </enumlist>
86  </parameter>
87  </syntax>
88  <description>
89  <para>Returns information from a given parameter.</para>
90  </description>
91  </function>
92  ***/
93 
94 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
95  char *buf, size_t len)
96 {
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");
101  return -1;
102  }
103 #endif
104  if (ast_strlen_zero(data)) {
105  ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
106  return -1;
107  } else if (!strcasecmp("loadavg", data)) {
108  double curloadavg;
109  getloadavg(&curloadavg, 1);
110  snprintf(buf, len, "%f", curloadavg);
111  } else if (!strcasecmp("numcalls", data)) {
112  snprintf(buf, len, "%d", ast_active_calls());
113  }
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, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024);
119  } else if (!strcasecmp("freeram", data)) { /* in KiB */
120  snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024);
121  } else if (!strcasecmp("bufferram", data)) { /* in KiB */
122  snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024);
123  } else if (!strcasecmp("totalswap", data)) { /* in KiB */
124  snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024);
125  } else if (!strcasecmp("freeswap", data)) { /* in KiB */
126  snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024);
127  } else if (!strcasecmp("numprocs", data)) {
128  snprintf(buf, len, "%d", sys_info.procs);
129  }
130 #endif
131  else {
132  ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
133  return -1;
134  }
135 
136  return 0;
137 }
138 
140  .name = "SYSINFO",
141  .read = sysinfo_helper,
142  .read_max = 22,
143 };
144 
145 static int unload_module(void)
146 {
147  return ast_custom_function_unregister(&sysinfo_function);
148 }
149 
150 static int load_module(void)
151 {
152  return ast_custom_function_register(&sysinfo_function);
153 }
154 
155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
156 
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
int ast_active_calls(void)
Retrieve the number of active calls.
Definition: pbx.c:5931
#define LOG_WARNING
Definition: logger.h:144
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static int unload_module(void)
Definition: func_sysinfo.c:145
static int load_module(void)
Definition: func_sysinfo.c:150
static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_sysinfo.c:94
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Data structure associated with a custom dialplan function.
Definition: pbx.h:95
Core PBX routines and definitions.
#define LOG_ERROR
Definition: logger.h:155
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
const char * name
Definition: pbx.h:96
static struct ast_custom_function sysinfo_function
Definition: func_sysinfo.c:139
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
int getloadavg(double *list, int nelem)
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180