func_sysinfo.c 4.7 KB

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