Changeset 4b96846179a35015ac0b22d5fe9e9f92480f06a5

Show
Ignore:
Timestamp:
03/17/08 04:06:17 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1205726777 +0000
git-parent:

[065b0349f122c8e5f44538bed5636ea8b49362b6]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1205726777 +0000
Message:

check code consolidation. allow modules to be more terse and add convenience functions to make writing completely asynch checkers much easier. (add a postgres proof-of-concept asynch checker

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • configure.in

    r125a593 r4b96846  
    103103        ] 
    104104) 
    105 AC_TRY_LINK([#include <libpq-fe.h>], 
     105AC_TRY_LINK([ 
     106        #include <libpq-fe.h> 
     107        #include <postgres.h> 
     108        #include <fmgr.h> 
     109        ], 
    106110        [ PGconn *dbh = PQconnectdb(""); ], 
    107111        [], 
  • src/eventer/eventer.h

    ref59bc7 r4b96846  
    88 
    99#include "noit_defines.h" 
     10#include "utils/noit_log.h" 
    1011#include <sys/time.h> 
    1112#include <sys/socket.h> 
     
    8586#endif 
    8687eventer_impl_t __eventer; 
     88noit_log_stream_t eventer_err; 
     89noit_log_stream_t eventer_deb; 
    8790 
    8891API_EXPORT(int) eventer_choose(const char *name); 
  • src/eventer/eventer_impl.c

    rb5f532b r4b96846  
    2222 
    2323eventer_impl_t __eventer = NULL; 
    24  
     24noit_log_stream_t eventer_err = NULL; 
     25noit_log_stream_t eventer_deb = NULL; 
    2526 
    2627static int __default_queue_threads = 5; 
     
    5253  int i; 
    5354  eventer_t e; 
     55 
     56  eventer_err = noit_log_stream_find("error/eventer"); 
     57  eventer_deb = noit_log_stream_find("debug/eventer"); 
     58  if(!eventer_err) eventer_err = noit_stderr; 
     59  if(!eventer_deb) eventer_deb = noit_debug; 
     60 
    5461  eventer_ssl_init(); 
    5562  eventer_jobq_init(&__global_backq); 
  • src/eventer/eventer_kqueue_impl.c

    r171d8ed r4b96846  
    306306                      NULL, 0, 
    307307                      &__zerotime); 
    308       noitLT(noit_debug, &__now, "debug: kevent(%d, [], %d) => %d\n", kqueue_fd, master_kqs->__ke_vec_used, fd_cnt); 
     308      noitLT(eventer_deb, &__now, "debug: kevent(%d, [], %d) => %d\n", kqueue_fd, master_kqs->__ke_vec_used, fd_cnt); 
    309309      if(fd_cnt < 0) { 
    310         noitLT(noit_error, &__now, "kevent: %s\n", strerror(errno)); 
     310        noitLT(eventer_err, &__now, "kevent: %s\n", strerror(errno)); 
    311311      } 
    312312      master_kqs->__ke_vec_used = 0; 
     
    320320                    ke_vec, ke_vec_a, 
    321321                    &__kqueue_sleeptime); 
    322     noitLT(noit_debug, &__now, "debug: kevent(%d, [], %d) => %d\n", kqueue_fd, ke_vec_used, fd_cnt); 
     322    noitLT(eventer_deb, &__now, "debug: kevent(%d, [], %d) => %d\n", kqueue_fd, ke_vec_used, fd_cnt); 
    323323    ke_vec_used = 0; 
    324324    if(fd_cnt < 0) { 
    325       noitLT(noit_error, &__now, "kevent: %s\n", strerror(errno)); 
     325      noitLT(eventer_err, &__now, "kevent: %s\n", strerror(errno)); 
    326326    } 
    327327    else { 
     
    353353        if(ke->flags & EV_ERROR) { 
    354354          if(ke->data != EBADF) 
    355             noitLT(noit_error, &__now, "error: %s\n", strerror(ke->data)); 
     355            noitLT(eventer_err, &__now, "error: %s\n", strerror(ke->data)); 
    356356          continue; 
    357357        } 
     
    366366        oldmask = e->mask; 
    367367        cbname = eventer_name_for_callback(e->callback); 
    368         noitLT(noit_debug, &__now, "kqueue: fire on %d/%x to %s(%p)\n", 
     368        noitLT(eventer_deb, &__now, "kqueue: fire on %d/%x to %s(%p)\n", 
    369369               fd, masks[fd], cbname?cbname:"???", e->callback); 
    370370        newmask = e->callback(e, masks[fd], e->closure, &__now); 
  • src/modules/Makefile.in

    ref59bc7 r4b96846  
    1212RANLIB=@RANLIB@ 
    1313LIBS=@LIBS@ 
     14PGLIBS=@PGLIBS@ 
    1415INSTALL=@INSTALL@ 
    1516 
    1617top_srcdir=@top_srcdir@ 
    1718 
    18 MODULES=ping_icmp.@MODULEEXT@ http.@MODULEEXT@ @BUILD_MODULES@ 
     19MODULES=ping_icmp.@MODULEEXT@ http.@MODULEEXT@ postgres.@MODULEEXT@ \ 
     20        @BUILD_MODULES@ 
    1921 
    2022all:    $(MODULES) 
     23 
     24postgres.@MODULEEXT@:   postgres.lo 
     25        @$(MODULELD) $(LDFLAGS) -o $@ postgres.lo @SERFLIBS@ ../serf/.libs/libserf-0.a $(PGLIBS) -lz -lssl -lcrypto 
     26        @echo "- linking $@" 
    2127 
    2228http.@MODULEEXT@:       http.lo 
  • src/modules/http.c

    r9a6060c r4b96846  
    344344  if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt); 
    345345} 
    346 static void serf_check_info_cleanup(serf_check_info_t *ci) { 
     346static void serf_cleanup(noit_module_t *self, noit_check_t *check) { 
     347  serf_check_info_t *ci; 
     348  ci = check->closure; 
    347349  if(ci->connection) { 
    348350    serf_connection_close(ci->connection); 
     
    361363                         void *closure, struct timeval *now) { 
    362364  serf_closure_t *ccl = (serf_closure_t *)closure; 
    363   serf_check_info_t *ci = (serf_check_info_t *)ccl->check->closure; 
    364365 
    365366  noitLT(nldeb, now, "serf_complete(%s)\n", ccl->check->target); 
     
    367368    generic_log_results(ccl->self, ccl->check); 
    368369  } 
    369   serf_check_info_cleanup(ci); 
     370  serf_cleanup(ccl->self, ccl->check); 
    370371  ccl->check->flags &= ~NP_RUNNING; 
    371372  free(ccl); 
     
    744745  return 0; 
    745746} 
    746 static void serf_cleanup(noit_module_t *self, noit_check_t *check) { 
    747   serf_check_info_t *sci; 
    748   if(check->fire_event) { 
    749     eventer_remove(check->fire_event); 
    750     free(check->fire_event->closure); 
    751     eventer_free(check->fire_event); 
    752     check->fire_event = NULL; 
    753   } 
    754   sci = check->closure; 
    755   if(sci) { 
    756     serf_check_info_cleanup(sci); 
    757     free(sci); 
    758   } 
    759 } 
    760747static int serf_initiate_check(noit_module_t *self, noit_check_t *check, 
    761748                               int once, noit_check_t *cause) { 
    762749  if(!check->closure) check->closure = calloc(1, sizeof(serf_check_info_t)); 
    763   if(once) { 
    764     serf_initiate(self, check); 
    765     return 0; 
    766   } 
    767   /* If check->fire_event, we're already scheduled... */ 
    768   if(!check->fire_event) { 
    769     struct timeval epoch = { 0L, 0L }; 
    770     noit_check_fake_last_check(check, &epoch, NULL); 
    771     noit_check_schedule_next(self, &epoch, check, NULL, serf_initiate); 
    772   } 
     750  INITIATE_CHECK(serf_initiate, self, check); 
    773751  return 0; 
    774752} 
     
    777755  /* resmon_check_info_t gives us a bit more space */ 
    778756  if(!check->closure) check->closure = calloc(1, sizeof(resmon_check_info_t)); 
    779   if(once) { 
    780     serf_initiate(self, check); 
    781     return 0; 
    782   } 
    783   if(!check->fire_event) { 
    784     struct timeval epoch = { 0L, 0L }; 
    785     noit_check_fake_last_check(check, &epoch, NULL); 
    786     noit_check_schedule_next(self, &epoch, check, NULL, serf_initiate); 
    787   } 
     757  INITIATE_CHECK(serf_initiate, self, check); 
    788758  return 0; 
    789759} 
     
    791761static void resmon_cleanup(noit_module_t *self, noit_check_t *check) { 
    792762  resmon_check_info_t *rci; 
    793   if(check->fire_event) { 
    794     eventer_remove(check->fire_event); 
    795     free(check->fire_event->closure); 
    796     eventer_free(check->fire_event); 
    797     check->fire_event = NULL; 
    798   } 
    799763  rci = check->closure; 
    800764  if(rci) { 
     
    803767    if(rci->resserv) free(rci->resserv); 
    804768    if(rci->xml_doc) xmlFreeDoc(rci->xml_doc); 
    805     serf_check_info_cleanup(&rci->serf); 
    806     free(rci); 
     769    serf_cleanup(self, check); 
    807770  } 
    808771} 
     
    843806    return 0; 
    844807  } 
    845   if(once) { 
    846     serf_initiate(self, check); 
    847     return 0; 
    848   } 
    849   if(!check->fire_event) 
    850     noit_check_schedule_next(self, NULL, check, NULL, serf_initiate); 
    851   return 0; 
    852 
    853  
    854  
     808  INITIATE_CHECK(serf_initiate, self, check); 
     809  return 0; 
     810
    855811 
    856812static int serf_onload(noit_module_t *self) { 
  • src/modules/ping_icmp.c

    r43a88c5 r4b96846  
    229229 
    230230  if ((proto = getprotobyname("icmp")) == NULL) { 
    231     noitL(nlerr, "Couldn't find 'icmp' protocol\n"); 
     231    noitL(noit_error, "Couldn't find 'icmp' protocol\n"); 
    232232    return -1; 
    233233  } 
     
    235235  data->ipv4_fd = socket(AF_INET, SOCK_RAW, proto->p_proto); 
    236236  if(data->ipv4_fd < 0) { 
    237     noitL(nlerr, "ping_icmp: socket failed: %s\n", 
     237    noitL(noit_error, "ping_icmp: socket failed: %s\n", 
    238238          strerror(errno)); 
    239239  } 
     
    258258      close(data->ipv4_fd); 
    259259      data->ipv4_fd = -1; 
    260       noitL(nlerr, 
     260      noitL(noit_error, 
    261261            "ping_icmp: could not set socket non-blocking: %s\n", 
    262262            strerror(errno)); 
     
    275275  data->ipv6_fd = socket(AF_INET6, SOCK_RAW, proto->p_proto); 
    276276  if(data->ipv6_fd < 0) { 
    277     noitL(nlerr, "ping_icmp: socket failed: %s\n", 
     277    noitL(noit_error, "ping_icmp: socket failed: %s\n", 
    278278          strerror(errno)); 
    279279  } 
     
    283283      close(data->ipv6_fd); 
    284284      data->ipv6_fd = -1; 
    285       noitL(nlerr, 
     285      noitL(noit_error, 
    286286            "ping_icmp: could not set socket non-blocking: %s\n", 
    287287               strerror(errno)); 
     
    346346static void ping_check_cleanup(noit_module_t *self, noit_check_t *check) { 
    347347  struct check_info *ci = (struct check_info *)check->closure; 
    348   if(check->fire_event) { 
    349     eventer_remove(check->fire_event); 
    350     free(check->fire_event->closure); 
    351     eventer_free(check->fire_event); 
    352     check->fire_event = NULL; 
    353   } 
    354348  if(ci) { 
    355349    if(ci->timeout_event) { 
     
    360354    } 
    361355    if(ci->turnaround) free(ci->turnaround); 
    362     free(ci); 
    363356  } 
    364357} 
     
    459452                                    int once, noit_check_t *cause) { 
    460453  if(!check->closure) check->closure = calloc(1, sizeof(struct check_info)); 
    461   if(once) { 
    462     ping_icmp_send(self, check); 
    463     return 0; 
    464   } 
    465   if(!check->fire_event) { 
    466     struct timeval epoch; 
    467     noit_check_fake_last_check(check, &epoch, NULL); 
    468     noit_check_schedule_next(self, &epoch, check, NULL, ping_icmp_send); 
    469   } 
     454  INITIATE_CHECK(ping_icmp_send, self, check); 
    470455  return 0; 
    471456} 
  • src/modules/snmp.c

    r9a6060c r4b96846  
    351351 
    352352  /* Create a hash of important check attributes */ 
     353  noit_check_make_attrs(check, &check_attrs_hash); 
    353354#define CA_STORE(a,b) noit_hash_store(&check_attrs_hash, a, strlen(a), b) 
    354355  CA_STORE("target", check->target); 
     
    449450                                    int once, noit_check_t *cause) { 
    450451  if(!check->closure) check->closure = calloc(1, sizeof(struct check_info)); 
    451   if(once) { 
    452     noit_snmp_send(self, check); 
    453     return 0; 
    454   } 
    455   if(!check->fire_event) { 
    456     struct timeval epoch; 
    457     noit_check_fake_last_check(check, &epoch, NULL); 
    458     noit_check_schedule_next(self, &epoch, check, NULL, noit_snmp_send); 
    459   } 
     452  INITIATE_CHECK(noit_snmp_send, self, check); 
    460453  return 0; 
    461454} 
     
    465458} 
    466459static int noit_snmp_onload(noit_module_t *self) { 
    467   nlerr = noit_log_stream_find("error/noit_snmp"); 
    468   nldeb = noit_log_stream_find("debug/noit_snmp"); 
     460  nlerr = noit_log_stream_find("error/snmp"); 
     461  nldeb = noit_log_stream_find("debug/snmp"); 
    469462  if(!nlerr) nlerr = noit_stderr; 
    470463  if(!nldeb) nldeb = noit_debug; 
  • src/modules/ssh2.c

    rf239498 r4b96846  
    4141static noit_log_stream_t nldeb = NULL; 
    4242 
    43 static void ssh2_check_info_cleanup(ssh2_check_info_t *ci) { 
    44   if(ci->timeout_event) { 
    45     eventer_remove(ci->timeout_event); 
    46     eventer_free(ci->timeout_event); 
    47   } 
    48   if(ci->session) { 
    49     libssh2_session_disconnect(ci->session, "Bye!"); 
    50     libssh2_session_free(ci->session); 
    51   } 
    52   if(ci->error) free(ci->error); 
    53   memset(ci, 0, sizeof(*ci)); 
     43static void ssh2_cleanup(noit_module_t *self, noit_check_t *check) { 
     44  ssh2_check_info_t *ci = check->closure; 
     45  if(ci) { 
     46    if(ci->timeout_event) { 
     47      eventer_remove(ci->timeout_event); 
     48      eventer_free(ci->timeout_event); 
     49    } 
     50    if(ci->session) { 
     51      libssh2_session_disconnect(ci->session, "Bye!"); 
     52      libssh2_session_free(ci->session); 
     53    } 
     54    if(ci->error) free(ci->error); 
     55    memset(ci, 0, sizeof(*ci)); 
     56  } 
    5457} 
    5558static int ssh2_init(noit_module_t *self) { 
     
    9194    noit_check_t *check = ci->check; 
    9295    ssh2_log_results(ci->self, ci->check); 
    93     ssh2_check_info_cleanup(ci); 
     96    ssh2_cleanup(ci->self, ci->check); 
    9497    eventer_remove_fd(e->fd); 
    9598    e->opset->close(e->fd, &mask, e); 
     
    143146    ci->error = strdup("ssh connection failed"); 
    144147    ssh2_log_results(ci->self, ci->check); 
    145     ssh2_check_info_cleanup(ci); 
     148    ssh2_cleanup(ci->self, ci->check); 
    146149    eventer_remove_fd(e->fd); 
    147150    e->opset->close(e->fd, &mask, e); 
     
    176179  ci->error = strdup("ssh connect timeout"); 
    177180  ssh2_log_results(ci->self, ci->check); 
    178   ssh2_check_info_cleanup(ci); 
     181  ssh2_cleanup(ci->self, ci->check); 
    179182  eventer_remove_fd(e->fd); 
    180183  e->opset->close(e->fd, &mask, e); 
     
    268271  if(fd >= 0) close(fd); 
    269272  ssh2_log_results(ci->self, ci->check); 
    270   ssh2_check_info_cleanup(ci); 
     273  ssh2_cleanup(ci->self, ci->check); 
    271274  check->flags &= ~NP_RUNNING; 
    272275  return -1; 
     
    275278static int ssh2_initiate_check(noit_module_t *self, noit_check_t *check, 
    276279                               int once, noit_check_t *parent) { 
    277   /* ssh2_check_info_t gives us a bit more space */ 
    278   ssh2_check_info_t *ci; 
    279280  if(!check->closure) check->closure = calloc(1, sizeof(ssh2_check_info_t)); 
    280   ci = check->closure; 
    281   ci->self = self; 
    282   ci->check = check; 
    283   if(once) { 
    284     ssh2_initiate(self, check); 
    285     return 0; 
    286   } 
    287   if(!check->fire_event) { 
    288     struct timeval epoch = { 0L, 0L }; 
    289     noit_check_fake_last_check(check, &epoch, NULL); 
    290     noit_check_schedule_next(self, &epoch, check, NULL, ssh2_initiate); 
    291   } 
    292   return 0; 
    293 
    294 static void ssh2_cleanup(noit_module_t *self, noit_check_t *check) { 
    295   ssh2_check_info_t *ci; 
    296   if(check->fire_event) { 
    297     eventer_remove(check->fire_event); 
    298     free(check->fire_event->closure); 
    299     eventer_free(check->fire_event); 
    300     check->fire_event = NULL; 
    301   } 
    302   ci = check->closure; 
    303   if(ci) { 
    304     ssh2_check_info_cleanup(ci); 
    305     free(ci); 
    306   } 
     281  INITIATE_CHECK(ssh2_initiate, self, check); 
     282  return 0; 
    307283} 
    308284 
  • src/noit.conf

    reacf8d5 r4b96846  
    1313    </console_output> 
    1414    <log name="feed" type="jlog" path="/var/log/noitd.feed(stratcon)"/> 
    15     <!--<log name="feed"><outlet name="stderr"/></log>--> 
     15    <components> 
     16      <error> 
     17        <outlet name="error"/> 
     18        <log name="error/eventer"/> 
     19        <log name="error/ping_icmp" disabled="true"/> 
     20        <log name="error/serf" disabled="true"/> 
     21        <log name="error/snmp" disabled="true"/> 
     22      </error> 
     23      <debug> 
     24        <log name="debug/eventer" disabled="true"/> 
     25        <log name="debug/ping_icmp" disabled="true"/> 
     26        <log name="debug/serf" disabled="true"/> 
     27        <log name="debug/snmp" disabled="true"/> 
     28      </debug> 
     29    </components> 
    1630    <feeds> 
    1731      <outlet name="feed"/> 
     
    5569  <checks max_initial_stutter="30000"> 
    5670    <check uuid="1b4e28ba-2fa1-11d2-883f-b9b761bde3fb" module="ping_icmp" target="10.80.116.4" period="15000" timeout="14000"/> 
    57     <dc1 timeout="30000" period="60000"> 
    58       <icmp module="ping_icmp" timeout="12000"> 
     71    <dc1 timeout="10" period="60000"> 
     72      <icmp module="ping_icmp" timeout="10"> 
    5973        <check uuid="1b4e28ba-2fa1-11d2-883f-b9a761bde3fb" target="66.225.209.7"/> 
    6074      </icmp> 
    61       <web module="http" timeout="8000"> 
     75      <web module="http" timeout="80"> 
    6276        <check uuid="1b4e28ba-2fa1-11d2-883f-b9a761bde3fc" target="66.225.209.31"> 
    6377          <config> 
     
    7387        </check> 
    7488      </web> 
    75       <resmon module="resmon" period="30000" timeout="5000"> 
     89      <resmon module="resmon" period="30000" timeout="10"> 
    7690        <check uuid="1b4e28ba-2fa1-11d2-883f-b9a761bde3fd" timeout="2000" target="10.225.209.36"/> 
    77         <parts module="resmon_part" period="0" timeout="1000" oncheck="resmon"> 
     91        <parts module="resmon_part" period="0" timeout="10" oncheck="resmon"> 
    7892          <check uuid="1b4e28ba-2fa1-11d2-883f-b9a761bde3fe" target="10.225.209.36"> 
    7993            <config> 
     
    8498        </parts> 
    8599      </resmon> 
    86       <switches module="snmp" period="30000"> 
     100      <switches module="snmp" period="60000"> 
    87101        <config inherit="SwitchPortX"/> 
    88102        <switch target="10.80.116.3"> 
     
    93107      </switches> 
    94108    </dc1> 
    95   <check uuid="002d58ff-20ff-4db0-9420-782fc1748dc4" module="ssh2" target="10.80.117.2" period="60000" timeout="4000"/></checks> 
     109  <check uuid="002d58ff-20ff-4db0-9420-782fc1748dc4" module="ssh2" target="10.80.117.2" period="60000" timeout="10"/></checks> 
    96110  <config_templates> 
    97111    <config id="SwitchPort"> 
  • src/noit_check.c

    ref59bc7 r4b96846  
    439439 
    440440  mod = noit_module_lookup(checker->module); 
    441   mod->cleanup(mod, checker); 
     441  if(mod->cleanup) mod->cleanup(mod, checker); 
    442442  if(checker->fire_event) { 
    443443     eventer_remove(checker->fire_event); 
     444     free(checker->fire_event->closure); 
    444445     eventer_free(checker->fire_event); 
    445446     checker->fire_event = NULL; 
    446447  } 
    447  
     448  if(checker->closure) free(checker->closure); 
    448449  if(checker->target) free(checker->target); 
    449450  if(checker->module) free(checker->module); 
  • src/noit_check_tools.c

    r9a6060c r4b96846  
    115115} 
    116116 
     117void 
     118noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) { 
     119  eventer_t e; 
     120  e = eventer_alloc(); 
     121  e->fd = -1; 
     122  e->mask = EVENTER_ASYNCH;  
     123  memcpy(&e->whence, &__now, sizeof(__now)); 
     124  p_int.tv_sec = check->timeout / 1000; 
     125  p_int.tv_usec = (check->timeout % 1000) * 1000; 
     126  add_timeval(e->whence, p_int, &e->whence); 
     127  e->callback = ssh2_connect_complete; 
     128  e->closure =  check->closure; 
     129  eventer_add(e); 
     130} 
     131 
     132void 
     133noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs) { 
     134#define CA_STORE(a,b) noit_hash_store(attrs, a, strlen(a), b) 
     135  CA_STORE("target", check->target); 
     136  CA_STORE("name", check->name); 
     137  CA_STORE("module", check->module); 
     138} 
     139void 
     140noit_check_release_attrs(noit_hash_table *attrs) { 
     141  noit_hash_destroy(attrs, NULL, NULL); 
     142} 
  • src/noit_check_tools.h

    r9a6060c r4b96846  
    2525                           struct timeval *now, dispatch_func_t recur); 
    2626 
     27API_EXPORT(void) 
     28  noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback); 
     29 
     30#define INITIATE_CHECK(func, self, check) do { \ 
     31  if(once) { \ 
     32    func(self, check); \ 
     33  } \ 
     34  else if(!check->fire_event) { \ 
     35    struct timeval epoch = { 0L, 0L }; \ 
     36    noit_check_fake_last_check(check, &epoch, NULL); \ 
     37    noit_check_schedule_next(self, &epoch, check, NULL, func); \ 
     38  } \ 
     39} while(0) 
     40 
     41API_EXPORT(void) 
     42  noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs); 
     43API_EXPORT(void) 
     44  noit_check_release_attrs(noit_hash_table *attrs); 
     45 
     46 
    2747#endif 
    2848