| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#if defined(HAVE_SYSINFO) |
| 35 |
#include <sys/sysinfo.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/pbx.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static int sysinfo_helper(struct ast_channel *chan, const char *cmd, char *data, |
| 96 |
char *buf, size_t len) |
| 97 |
{ |
| 98 |
#if defined(HAVE_SYSINFO) |
| 99 |
struct sysinfo sys_info; |
| 100 |
if (sysinfo(&sys_info)) { |
| 101 |
ast_log(LOG_ERROR, "FAILED to retrieve system information\n"); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
#endif |
| 105 |
if (ast_strlen_zero(data)) { |
| 106 |
ast_log(LOG_WARNING, "Syntax: ${SYSINFO(<parameter>)} - missing argument!)\n"); |
| 107 |
return -1; |
| 108 |
} else if (!strcasecmp("loadavg", data)) { |
| 109 |
double curloadavg; |
| 110 |
getloadavg(&curloadavg, 1); |
| 111 |
snprintf(buf, len, "%f", curloadavg); |
| 112 |
} else if (!strcasecmp("numcalls", data)) { |
| 113 |
snprintf(buf, len, "%d", ast_active_calls()); |
| 114 |
} |
| 115 |
#if defined(HAVE_SYSINFO) |
| 116 |
else if (!strcasecmp("uptime", data)) { |
| 117 |
snprintf(buf, len, "%ld", sys_info.uptime/3600); |
| 118 |
} else if (!strcasecmp("totalram", data)) { |
| 119 |
snprintf(buf, len, "%lu",(sys_info.totalram * sys_info.mem_unit)/1024); |
| 120 |
} else if (!strcasecmp("freeram", data)) { |
| 121 |
snprintf(buf, len, "%lu",(sys_info.freeram * sys_info.mem_unit)/1024); |
| 122 |
} else if (!strcasecmp("bufferram", data)) { |
| 123 |
snprintf(buf, len, "%lu",(sys_info.bufferram * sys_info.mem_unit)/1024); |
| 124 |
} else if (!strcasecmp("totalswap", data)) { |
| 125 |
snprintf(buf, len, "%lu",(sys_info.totalswap * sys_info.mem_unit)/1024); |
| 126 |
} else if (!strcasecmp("freeswap", data)) { |
| 127 |
snprintf(buf, len, "%lu",(sys_info.freeswap * sys_info.mem_unit)/1024); |
| 128 |
} else if (!strcasecmp("numprocs", data)) { |
| 129 |
snprintf(buf, len, "%d", sys_info.procs); |
| 130 |
} |
| 131 |
#endif |
| 132 |
else { |
| 133 |
ast_log(LOG_ERROR, "Unknown sysinfo parameter type '%s'.\n", data); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
static struct ast_custom_function sysinfo_function = { |
| 141 |
.name = "SYSINFO", |
| 142 |
.read = sysinfo_helper, |
| 143 |
.read_max = 22, |
| 144 |
}; |
| 145 |
|
| 146 |
static int unload_module(void) |
| 147 |
{ |
| 148 |
return ast_custom_function_unregister(&sysinfo_function); |
| 149 |
} |
| 150 |
|
| 151 |
static int load_module(void) |
| 152 |
{ |
| 153 |
return ast_custom_function_register(&sysinfo_function); |
| 154 |
} |
| 155 |
|
| 156 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "System information related functions"); |