00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
00035
00036 #if defined(HAVE_SYSINFO)
00037 #include <sys/sysinfo.h>
00038 #endif
00039
00040 #include "asterisk/module.h"
00041 #include "asterisk/pbx.h"
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data,
00095 char *buf, size_t len)
00096 {
00097 #if defined(HAVE_SYSINFO)
00098 struct sysinfo sys_info;
00099 if (sysinfo(&sys_info)) {
00100 ast_log(LOG_ERROR, "FAILED to retrieve system information\n");
00101 return -1;
00102 }
00103 #endif
00104 if (ast_strlen_zero(data)) {
00105 ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n");
00106 return -1;
00107 } else if (!strcasecmp("loadavg", data)) {
00108 double curloadavg;
00109 getloadavg(&curloadavg, 1);
00110 snprintf(buf, len, "%f", curloadavg);
00111 } else if (!strcasecmp("numcalls", data)) {
00112 snprintf(buf, len, "%d", ast_active_calls());
00113 }
00114 #if defined(HAVE_SYSINFO)
00115 else if (!strcasecmp("uptime", data)) {
00116 snprintf(buf, len, "%ld", sys_info.uptime/3600);
00117 } else if (!strcasecmp("totalram", data)) {
00118 snprintf(buf, len, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024);
00119 } else if (!strcasecmp("freeram", data)) {
00120 snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024);
00121 } else if (!strcasecmp("bufferram", data)) {
00122 snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024);
00123 } else if (!strcasecmp("totalswap", data)) {
00124 snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024);
00125 } else if (!strcasecmp("freeswap", data)) {
00126 snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024);
00127 } else if (!strcasecmp("numprocs", data)) {
00128 snprintf(buf, len, "%d", sys_info.procs);
00129 }
00130 #endif
00131 else {
00132 ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data);
00133 return -1;
00134 }
00135
00136 return 0;
00137 }
00138
00139 static struct ast_custom_function sysinfo_function = {
00140 .name = "SYSINFO",
00141 .read = sysinfo_helper,
00142 .read_max = 22,
00143 };
00144
00145 static int unload_module(void)
00146 {
00147 return ast_custom_function_unregister(&sysinfo_function);
00148 }
00149
00150 static int load_module(void)
00151 {
00152 return ast_custom_function_register(&sysinfo_function);
00153 }
00154
00155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions");
00156