fixes some coding guideline issue
[asterisk/asterisk.git] / funcs / func_sysinfo.c
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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 87233 $")
31
32 #if defined(HAVE_SYSINFO)
33 #include <sys/sysinfo.h>
34 #endif
35
36 #include "asterisk/module.h"
37 #include "asterisk/pbx.h"
38
39 /*** DOCUMENTATION
40         <function name="SYSINFO" language="en_US">
41                 <synopsis>
42                         Returns system information specified by parameter.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="parameter" required="true">
46                                 <enumlist>
47                                         <enum name="loadavg">
48                                                 <para>System load average from past minute.</para>
49                                         </enum>
50                                         <enum name="numcalls">
51                                                 <para>Number of active calls currently in progress.</para>
52                                         </enum>
53                                         <enum name="uptime">
54                                                 <para>System uptime in hours.</para>
55                                                 <note><para>This parameter is dependant upon operating system.</para></note>
56                                         </enum>
57                                         <enum name="totalram">
58                                                 <para>Total usable main memory size in KiB.</para>
59                                                 <note><para>This parameter is dependant upon operating system.</para></note>
60                                         </enum>
61                                         <enum name="freeram">
62                                                 <para>Available memory size in KiB.</para>
63                                                 <note><para>This parameter is dependant upon operating system.</para></note>
64                                         </enum>
65                                         <enum name="bufferram">
66                                                 <para>Memory used by buffers in KiB.</para>
67                                                 <note><para>This parameter is dependant upon operating system.</para></note>
68                                         </enum>
69                                         <enum name="totalswap">
70                                                 <para>Total swap space still available in KiB.</para>
71                                                 <note><para>This parameter is dependant upon operating system.</para></note>
72                                         </enum>
73                                         <enum name="freeswap">
74                                                 <para>Free swap space still available in KiB.</para>
75                                                 <note><para>This parameter is dependant upon operating system.</para></note>
76                                         </enum>
77                                         <enum name="numprocs">
78                                                 <para>Number of current processes.</para>
79                                                 <note><para>This parameter is dependant upon operating system.</para></note>
80                                         </enum>
81                                 </enumlist>
82                         </parameter>
83                 </syntax>
84                 <description>
85                         <para>Returns information from a given parameter.</para>
86                 </description>
87         </function>
88  ***/
89
90 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
91                                          char *buf, size_t len)
92 {
93 #if defined(HAVE_SYSINFO)
94         struct sysinfo sys_info;
95         if (sysinfo(&sys_info)) {
96                 ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
97                 return -1;
98         }
99 #endif
100         if (ast_strlen_zero(data)) {
101                 ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
102                 return -1;
103         } else if (!strcasecmp("loadavg", data)) {
104                 double curloadavg;
105                 getloadavg(&curloadavg, 1);
106                 snprintf(buf, len, "%f", curloadavg);
107         } else if (!strcasecmp("numcalls", data)) {
108                 snprintf(buf, len, "%d", ast_active_calls());
109         }
110 #if defined(HAVE_SYSINFO)
111         else if (!strcasecmp("uptime", data)) {             /* in hours */
112                 snprintf(buf, len, "%ld", sys_info.uptime/3600);
113         } else if (!strcasecmp("totalram", data)) {         /* in KiB */
114                 snprintf(buf, len, "%ld",(sys_info.totalram * sys_info.mem_unit)/1024);
115         } else if (!strcasecmp("freeram", data)) {          /* in KiB */
116                 snprintf(buf, len, "%ld",(sys_info.freeram * sys_info.mem_unit)/1024);
117         } else if (!strcasecmp("bufferram", data)) {        /* in KiB */
118                 snprintf(buf, len, "%ld",(sys_info.bufferram * sys_info.mem_unit)/1024);
119         } else if (!strcasecmp("totalswap", data)) {        /* in KiB */
120                 snprintf(buf, len, "%ld",(sys_info.totalswap * sys_info.mem_unit)/1024);
121         } else if (!strcasecmp("freeswap", data)) {         /* in KiB */
122                 snprintf(buf, len, "%ld",(sys_info.freeswap * sys_info.mem_unit)/1024);
123         } else if (!strcasecmp("numprocs", data)) {
124                 snprintf(buf, len, "%d", sys_info.procs);
125         }
126 #endif
127         else {
128                 ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
129                 return -1;
130         }
131                 
132         return 0;
133 }
134
135 static struct ast_custom_function sysinfo_function = {
136         .name = "SYSINFO",
137         .read = sysinfo_helper,
138         .read_max = 22,
139 };
140
141 static int unload_module(void)
142 {
143         return ast_custom_function_unregister(&sysinfo_function);
144 }
145
146 static int load_module(void)
147 {
148         return ast_custom_function_register(&sysinfo_function);
149 }
150
151 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
152