Make the SYSINFO documentation reflect which options were compiled in
[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 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
40                                          char *buf, size_t len)
41 {
42 #if defined(HAVE_SYSINFO)
43         struct sysinfo sys_info;
44         if (sysinfo(&sys_info)) {
45                 ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
46                 return -1;
47         }
48 #endif
49         if (ast_strlen_zero(data)) {
50                 ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
51                 return -1;
52         } else if (!strcasecmp("loadavg", data)) {
53                 double curloadavg;
54                 getloadavg(&curloadavg, 1);
55                 snprintf(buf, len, "%f", curloadavg);
56         } else if (!strcasecmp("numcalls", data)) {
57                 snprintf(buf, len, "%d", ast_active_calls());
58         }
59 #if defined(HAVE_SYSINFO)
60         else if (!strcasecmp("uptime", data)) {             /* in hours */
61                 snprintf(buf, len, "%ld", sys_info.uptime/3600);
62         } else if (!strcasecmp("totalram", data)) {         /* in KiB */
63                 snprintf(buf, len, "%ld",(sys_info.totalram / sys_info.mem_unit)/1024);
64         } else if (!strcasecmp("freeram", data)) {          /* in KiB */
65                 snprintf(buf, len, "%ld",(sys_info.freeram / sys_info.mem_unit)/1024);
66         } else if (!strcasecmp("bufferram", data)) {        /* in KiB */
67                 snprintf(buf, len, "%ld",(sys_info.bufferram / sys_info.mem_unit)/1024);
68         } else if (!strcasecmp("totalswap", data)) {        /* in KiB */
69                 snprintf(buf, len, "%ld",(sys_info.totalswap / sys_info.mem_unit)/1024);
70         } else if (!strcasecmp("freeswap", data)) {         /* in KiB */
71                 snprintf(buf, len, "%ld",(sys_info.freeswap / sys_info.mem_unit)/1024);
72         } else if (!strcasecmp("numprocs", data)) {
73                 snprintf(buf, len, "%d", sys_info.procs);
74         }
75 #endif
76         else {
77                 ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
78                 return -1;
79         }
80                 
81         return 0;
82 }
83
84 static struct ast_custom_function sysinfo_function = {
85         .name = "SYSINFO",
86         .synopsis = "Returns system information specified by parameter.",
87         .syntax = "SYSINFO(<parameter>)",
88         .read = sysinfo_helper,
89         .desc = 
90 "Returns information from a given parameter\n"
91 "  Options:\n"
92 "    loadavg   - system load average from past minute\n"
93 "    numcalls  - number of active calls currently in progress\n"
94 #if defined(HAVE_SYSINFO)
95 "    uptime    - system uptime in hours\n"
96 "    totalram  - total usable main memory size in KiB\n"
97 "    freeram   - available memory size in KiB\n"
98 "    bufferram - memory used by buffers in KiB\n"
99 "    totalswap - total swap space size in KiB\n"
100 "    freeswap  - free swap space still available in KiB\n"
101 "    numprocs  - number of current processes\n",
102 #endif /* HAVE_SYSINFO */
103 };
104
105 static int unload_module(void)
106 {
107         return ast_custom_function_unregister(&sysinfo_function);
108 }
109
110 static int load_module(void)
111 {
112         return ast_custom_function_register(&sysinfo_function);
113 }
114
115 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
116