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