summaryrefslogtreecommitdiff
path: root/src/lbtest_user.cc
diff options
context:
space:
mode:
authorMichael Larson <mike@vyatta.com>2011-03-30 12:09:58 -0700
committerMichael Larson <mike@vyatta.com>2011-03-30 12:09:58 -0700
commit25eef8bfb91c2c57907ab79f4ba6bcd9fe0064cb (patch)
tree53f6756f29c645fcec455bfec6a1bf8bdb53e50d /src/lbtest_user.cc
parent55cab2fbbf2ee75d6939a9210ddcaa5c72ed6981 (diff)
downloadvyatta-wanloadbalance-25eef8bfb91c2c57907ab79f4ba6bcd9fe0064cb.tar.gz
vyatta-wanloadbalance-25eef8bfb91c2c57907ab79f4ba6bcd9fe0064cb.zip
check in for Bug 6959
Add the ability to run a user defined script to determine wan load-balancing interface health includes sample http test (scripts/http_test.pl). Configuration will look as follows: wan { interface-health eth0 { nexthop 10.3.0.1 test 1 { test-script /tmp/http_test.pl type user-defined } } interface-health eth1 { nexthop 10.3.0.1 test 1 { test-script /tmp/http_test.pl type user-defined } } With corresponding output: vyatta@vyatta# run show wan-load-balance Interface: eth0 Status: active Last Status Change: Wed Mar 30 19:03:59 2011 +Test: user Script: /tmp/http_test.pl Last Interface Success: 0s Last Interface Failure: n/a # Interface Failure(s): 0 Interface: eth1 Status: failed Last Status Change: Wed Mar 30 19:03:59 2011 -Test: user Script: /tmp/http_test.pl Last Interface Success: n/a Last Interface Failure: 0s # Interface Failure(s): 52 Scripts returns 0 for success, non-zero for failure.
Diffstat (limited to 'src/lbtest_user.cc')
-rw-r--r--src/lbtest_user.cc146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/lbtest_user.cc b/src/lbtest_user.cc
new file mode 100644
index 0000000..4dbed74
--- /dev/null
+++ b/src/lbtest_user.cc
@@ -0,0 +1,146 @@
+/*
+ * Module: lbtest_user.cc
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ */
+#include <syslog.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <string>
+#include <algorithm>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "lbdata.hh"
+#include "lbtest_user.hh"
+
+using namespace std;
+
+/**
+ *
+ *
+ **/
+void
+LBTestUser::send(LBHealth &health)
+{
+ if (_debug) {
+ cout << "LBTestUser::process()" << endl;
+ }
+
+ _status_line = name() + "\t" + string("Script: ") + _script;
+
+
+ //need to set environment variable for interface
+
+ //execute script
+ string environment = "export WLB_SCRIPT_IFACE=" + health._interface;
+ string cmd = "/bin/bash -p -c '" + environment + ";umask 000; " + _script + ";'";
+ string out;
+ if (system_out(cmd,out) == 0) {
+ _state = LBTest::K_SUCCESS;
+ }
+ else {
+ _state = LBTest::K_FAILURE;
+ }
+ return;
+}
+
+int
+LBTestUser::system_out(const string &cmd, string &out)
+{
+ // fprintf(out_stream,"system out\n");
+ if (cmd.empty()) {
+ return -1;
+ }
+
+ int cp[2]; // Child to parent pipe
+ if( pipe(cp) < 0) {
+ return -1;
+ }
+
+ pid_t pid = fork();
+ if (pid == 0) {
+ //child
+ close(cp[0]);
+ close(0); // Close current stdin.
+ dup2(cp[1],STDOUT_FILENO); // Make stdout go to write end of pipe.
+ dup2(cp[1],STDERR_FILENO); // Make stderr go to write end of pipe.
+ // fcntl(cp[1],F_SETFD,fcntl(cp[1],F_GETFD) & (~FD_CLOEXEC));
+ close(cp[1]);
+ int ret = 0;
+
+
+ //set user and group here
+ setregid(getegid(),getegid());
+ setreuid(geteuid(),geteuid());
+
+ // fprintf(out_stream,"executing: %s\n",cmd);
+ if (execl("/bin/sh","sh","-c",cmd.c_str(),NULL) == -1) {
+ ret = errno;
+ }
+ close(cp[1]);
+ exit(ret);
+ }
+ else {
+ //parent
+ char buf[8192];
+ memset(buf,'\0',8192);
+ close(cp[1]);
+ fd_set rfds;
+ struct timeval tv;
+
+ int flags = fcntl(cp[0], F_GETFL, 0);
+ fcntl(cp[0], F_SETFL, flags | O_NONBLOCK);
+
+ FD_ZERO(&rfds);
+ FD_SET(cp[0], &rfds);
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+ while (select(FD_SETSIZE, &rfds, NULL, NULL, &tv) != -1) {
+ int err = 0;
+ if ((err = read(cp[0], &buf, 8191)) > 0) {
+ out += string(buf);
+ }
+ else if (err == -1 && errno == EAGAIN) {
+ //try again
+ }
+ else {
+ break;
+ }
+ FD_ZERO(&rfds);
+ FD_SET(cp[0], &rfds);
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ fflush(NULL);
+ }
+
+ //now wait on child to kick the bucket
+ int status;
+ wait(&status);
+ close(cp[0]);
+ return WEXITSTATUS(status);
+ }
+}
+
+
+/**
+ *
+ *
+ **/
+string
+LBTestUser::dump()
+{
+ char buf[20];
+ sprintf(buf,"%u",_resp_time);
+ string foo = string("script: ") + _script + ", resp_time: " + buf;
+ return foo;
+}