rrd based system stats
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

214 lines
5.3 KiB

  1. /***************************************************************************
  2. * Copyright (C) 02/2010 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/socket.h>
  25. #include <sys/types.h>
  26. #include <sys/un.h>
  27. #include "configfile.h"
  28. #include "helper.h"
  29. #include "list.h"
  30. #include "logging.h"
  31. #include "plugins.h"
  32. #include "probe.h"
  33. #include "sockaddr.h"
  34. #define TS2_RESP_SIZE 1024
  35. struct sammler_plugin plugin;
  36. struct server_entry {
  37. struct list_head list;
  38. char *name;
  39. int errors;
  40. struct sockaddr_in sa;
  41. };
  42. static LIST_HEAD(server_list);
  43. static char *ts2_resp_buf;
  44. static const char *ds_def = {
  45. "DS:users:GAUGE:90:0:U "
  46. "DS:channels:GAUGE:90:0:U "
  47. };
  48. static const char * get_ds(int ds_id)
  49. {
  50. return ds_def;
  51. }
  52. static int probe(void)
  53. {
  54. struct server_entry *entry;
  55. list_for_each_entry(entry, &server_list, list) {
  56. int sock = socket(AF_INET, SOCK_STREAM, 0);
  57. if (sock == -1) {
  58. log_print(LOG_WARN, "plugin ts2: socket()");
  59. continue;
  60. }
  61. int ret = connect(sock, (struct sockaddr *)&entry->sa, sizeof(entry->sa));
  62. if (ret != 0) {
  63. if (entry->errors++ == 0)
  64. log_print(LOG_ERROR, "plugin ts2: connect(%s)", get_sockaddr_buf(&entry->sa));
  65. close(sock);
  66. continue;
  67. }
  68. /* read prompt "[TS]" */
  69. int len = read(sock, ts2_resp_buf, TS2_RESP_SIZE);
  70. if (len <= 0) {
  71. if (entry->errors++ == 0)
  72. log_print(LOG_ERROR, "ts2: read(1)");
  73. close(sock);
  74. continue;
  75. }
  76. if (strncmp(ts2_resp_buf, "[TS]", 4) != 0) {
  77. if (entry->errors++ == 0)
  78. log_print(LOG_ERROR, "ts2: invalid prompt from server");
  79. close(sock);
  80. continue;
  81. }
  82. /* query "general informations" */
  83. char cmd1[] = "gi\n";
  84. write(sock, cmd1, sizeof(cmd1));
  85. len = read(sock, ts2_resp_buf, TS2_RESP_SIZE);
  86. if (len <= 0) {
  87. if (entry->errors++ == 0)
  88. log_print(LOG_ERROR, "ts2: read(2)");
  89. close(sock);
  90. continue;
  91. }
  92. /* send quit (but we close the socket anyway..) */
  93. char cmd2[] = "quit";
  94. write(sock, cmd2, sizeof(cmd2));
  95. close(sock);
  96. /* terminate response */
  97. ts2_resp_buf[len] = '\0';
  98. char *tok, *tmp, *resp = ts2_resp_buf;
  99. int users = 0, channels = 0;
  100. while ((tok = strtok_r(resp, "\r\n", &tmp)) != NULL) {
  101. /* total_users_online=14 */
  102. if (strncmp(tok, "total_users_online", 18) == 0) {
  103. users = atoi(tok +19);
  104. /* total_channels=3 */
  105. } else if (strncmp(tok, "total_channels", 14) == 0) {
  106. channels = atoi(tok +15);
  107. }
  108. resp = NULL;
  109. }
  110. char filename[32];
  111. len = snprintf(filename, sizeof(filename), "ts2-%s.rrd", entry->name);
  112. if (len < 0 || len >= sizeof(filename))
  113. continue;
  114. probe_submit(&plugin, filename, 0, "%d:%d", users, channels);
  115. if (entry->errors > 0) {
  116. log_print(LOG_ERROR, "plugin ts2: success (%s) after %d errors",
  117. entry->name, entry->errors);
  118. entry->errors = 0;
  119. }
  120. }
  121. return 0;
  122. }
  123. static int init_cb(struct strtoken *tokens, void *privdata)
  124. {
  125. if (tokens->count != 2) {
  126. log_print(LOG_ERROR, "p_ts2: parse error");
  127. return -1;
  128. }
  129. struct server_entry *entry = malloc(sizeof(struct server_entry));
  130. if (entry == NULL) {
  131. log_print(LOG_ERROR, "p_ts2: out of memory");
  132. return -1;
  133. }
  134. entry->name = strdup(tokens->field[0]);
  135. entry->errors = 0;
  136. if (parse_sockaddr(tokens->field[1], &entry->sa) < 0) {
  137. log_print(LOG_ERROR, "p_ts2: invalid address: <%s>", tokens->field[1]);
  138. free(entry->name);
  139. free(entry);
  140. return -1;
  141. }
  142. log_print(LOG_INFO, "p_ts2: added server '%s'", entry->name);
  143. list_add_tail(&entry->list, &server_list);
  144. return 0;
  145. }
  146. static int init(void)
  147. {
  148. ts2_resp_buf = malloc(TS2_RESP_SIZE);
  149. if (ts2_resp_buf == NULL) {
  150. log_print(LOG_ERROR, "p_ts2: out of memory");
  151. return -1;
  152. }
  153. config_get_strtokens("p_ts2", "server", ",", 2, init_cb, NULL);
  154. return 0;
  155. }
  156. static int fini(void)
  157. {
  158. struct server_entry *entry, *tmp;
  159. list_for_each_entry_safe(entry, tmp, &server_list, list) {
  160. free(entry->name);
  161. free(entry);
  162. }
  163. free(ts2_resp_buf);
  164. return 0;
  165. }
  166. struct sammler_plugin plugin = {
  167. .name = "ts2",
  168. .interval = 60,
  169. .init = &init,
  170. .fini = &fini,
  171. .probe = &probe,
  172. .get_ds = &get_ds,
  173. };