Advertisement
Guest User

Untitled

a guest
Sep 16th, 2011
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. /*
  2.  
  3. Instructions :
  4. ==============
  5.  
  6. Install : apxs -i -a -c mod_load.c
  7. Configure : MaxLoad apache directive (default is 25)
  8.  
  9.  
  10. Apache 2.0 version :
  11. --------------------
  12.  
  13. Copyright (C) 2005
  14. Planet-Work SARL
  15. http://www.planet-work.fr/
  16. Frederic VANNIERE - [email protected]
  17.  
  18. Original Apache 1.3 version :
  19. -----------------------------
  20.  
  21. Copyright (C) 2000,2001,2002,2003
  22. SedNove Inc.
  23. 439 OAK
  24. St-Lambert, QC, Canada
  25. All rights reserved.
  26. Pierre Laplante
  27. Wed Nov 20 15:53:45 2002
  28.  
  29. */
  30.  
  31. #include "httpd.h"
  32. #include "http_config.h"
  33. #include "http_core.h"
  34. #include "http_log.h"
  35. #include "http_protocol.h"
  36. #include "http_request.h"
  37. #include "http_main.h"
  38. #include "util_script.h"
  39. #include "util_md5.h"
  40. #include "ap_config.h"
  41. #include "unistd.h"
  42.  
  43. /*#define DEBUGLVL 9*/
  44.  
  45. #ifdef DEBUGLVL
  46. #define DEBUG(lvl,x...) if (lvl<DEBUGLVL) fprintf(stderr, x);
  47. #else
  48. #define DEBUG(lvl,x...)
  49. #endif
  50.  
  51. module AP_MODULE_DECLARE_DATA load_module;
  52.  
  53. typedef struct {
  54. int max_load;
  55. } load_handler_config;
  56.  
  57. static void *
  58. create_config(apr_pool_t *p, server_rec *s) {
  59. load_handler_config *c;
  60.  
  61. DEBUG(5, "create_config\n");
  62.  
  63. c = (load_handler_config *) apr_pcalloc(p, sizeof(load_handler_config));
  64. c -> max_load = 25;
  65.  
  66. DEBUG(5, "create_config c=%x.\n", c);
  67. return (void *) c;
  68. }
  69.  
  70.  
  71.  
  72. static const char *
  73. my_handler(cmd_parms *parms, void *mconfig, char *to) {
  74. load_handler_config *c;
  75.  
  76. DEBUG(5, "my_handler, to=%s, mconfig=%x, parms=%x/%x\n", to, mconfig, parms, parms);
  77. c = (load_handler_config *) ap_get_module_config(parms->server->module_config,
  78. &load_module);
  79.  
  80. DEBUG(5, "c=%x\n", c);
  81. c -> max_load = atoi(to);
  82. return NULL;
  83. }
  84.  
  85.  
  86. static const command_rec load_cmds[] =
  87. {
  88. AP_INIT_TAKE1("MaxLoad",my_handler,(void*) 0,RSRC_CONF,"Set max Loadavg." ),
  89. {NULL}
  90. };
  91.  
  92.  
  93. static int load_handler(request_rec *r) {
  94. int fd;
  95. float la;
  96. char buf[1024];
  97. load_handler_config *c;
  98.  
  99. DEBUG(5, "load_handler\n");
  100.  
  101. c = (load_handler_config *) ap_get_module_config(r->server->module_config,
  102. &load_module);
  103. DEBUG(5, "c=%x\n", c);
  104. DEBUG(5, "load = %d \n", c -> max_load);
  105. fd = open("/proc/loadavg", O_RDONLY);
  106. if (read(fd, buf, 1024) <=0) {
  107. //ap_log_reason("Can't open /proc/loadavg", "/proc/loadavg", r);
  108. ap_log_error(APLOG_MARK, APLOG_ERR,0,NULL, "Can't open /proc/loadavg");
  109. return DECLINED;
  110. } else {
  111. sscanf(buf, "%f", &la);
  112. if (la > c -> max_load) {
  113. ap_log_error(APLOG_MARK, APLOG_ERR,0,NULL,
  114. "load is too high %f/%d ...\n", la, c->max_load);
  115. return HTTP_SERVICE_UNAVAILABLE;
  116. }
  117. }
  118. close(fd);
  119.  
  120. return DECLINED;
  121. }
  122.  
  123. static void
  124. load_register_hooks (apr_pool_t * p)
  125. {
  126. ap_hook_access_checker(load_handler,NULL,NULL,APR_HOOK_FIRST);
  127. }
  128.  
  129. module AP_MODULE_DECLARE_DATA load_module =
  130. {
  131. STANDARD20_MODULE_STUFF,
  132. NULL, /* per-directory config creator */
  133. NULL, /* dir config merger */
  134. create_config, /* server config creator */
  135. NULL, /* server config merger */
  136. load_cmds, /* command table */
  137. load_register_hooks
  138. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement