summaryrefslogtreecommitdiff
path: root/src/lboutput.cc
blob: e673ee049c2d7f45c2a6d1abf979b62633ff210c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Module: lboutput.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 <sys/time.h>
#include <time.h>
#include <syslog.h>

#include <iostream>

#include "lbdata.hh"
#include "lboutput.hh"

void
LBOutput::shutdown()
{
  string wlb_out = _output_path + "/wlb.out";
  unlink(wlb_out.c_str());
}

void
LBOutput::write(const LBData &lbdata)
{
  timeval tv;
  gettimeofday(&tv,NULL);

  string wlb_out = _output_path + "/wlb.out";

  //open file
  FILE *fp = fopen(wlb_out.c_str(), "w");
  if (fp == NULL) {
    if (_debug) {
      cerr << "Error opening output file: " << wlb_out << endl;
    }
    syslog(LOG_ERR, "wan_lb: error ordering output file %s", wlb_out.c_str());
    return;
  }

  if (_debug) {
    //dump out the health data
    LBData::InterfaceHealthConstIter iter = lbdata._iface_health_coll.begin();
    while (iter != lbdata._iface_health_coll.end()) {
      if (_debug) {
	cout << iter->first << " "; //interface
	cout << string(iter->second._is_active ? "true" : "false") << " "; //status
	cout << tv.tv_sec - iter->second.last_success() << " "; //last success
	cout << tv.tv_sec - iter->second.last_failure() << " "; //last failure
	cout << endl;
      }
      ++iter;
    }
  }

  string space("  ");
  timeval cur_t;
  gettimeofday(&cur_t,NULL);
  LBData::InterfaceHealthConstIter iter = lbdata._iface_health_coll.begin();
  while (iter != lbdata._iface_health_coll.end()) {
    string line = string("Interface:  ") + iter->first + "\n";
    line += space + string("Status:  ") + string(iter->second._is_active?"active":"failed") + "\n";

    const time_t t = (time_t)(iter->second._last_time_state_changed);
    char *tbuf = ctime(&t);

    line += space + string("Last Status Change:  ") + string(tbuf);

    string target = iter->second._ping_target;
    if (target.empty()) {
      target = iter->second._nexthop;
    }

    line += space + string("Target:  Ping ") + target + "\n";

    char btmp[256];
    string time_buf;
    
    unsigned long diff_t;
    //the last condition is to handle a system time change...
    if (iter->second.last_success() > 0 && (cur_t.tv_sec > iter->second.last_success())) {
      diff_t = cur_t.tv_sec - iter->second.last_success();
    }
    else {
      diff_t = 0;
    }

    unsigned long days,hours,mins,secs;

    days = diff_t / (60*60*24);
    if (days > 0) {
      diff_t -= days * (60*60*24);
      sprintf(btmp,"%ld",days);
      time_buf += string(btmp) + "d";
    }

    hours = diff_t / (60*60);
    if (hours > 0) {
      diff_t -= hours * (60*60);
      sprintf(btmp,"%ld",hours);
      time_buf += string(btmp) + "h";
    }

    mins = diff_t / (60);
    if (mins > 0) {
      diff_t -= mins * (60);
      sprintf(btmp,"%ld",mins);
      time_buf += string(btmp) + "m";
    }

    secs = diff_t;
    sprintf(btmp,"%ld",secs);
    time_buf += string(btmp) + "s";

    string success_time;
    if (iter->second.last_success() == 0) {
      success_time = string("n/a") + string("\t\t");
    }
    else {
      success_time = time_buf + string("\t");
    }

    line += space + space + string("Last Ping Success:  ") + success_time + "\n";

    time_buf = "";

    if (iter->second.last_failure() > 0) {
      diff_t = cur_t.tv_sec - iter->second.last_failure();
    }
    else {
      diff_t = 0;
    }

    days = diff_t / (60*60*24);
    if (days > 0) {
      diff_t -= days * (60*60*24);
      sprintf(btmp,"%ld",days);
      time_buf += string(btmp) + "d";
    }

    hours = diff_t / (60*60);
    if (hours > 0) {
      diff_t -= hours * (60*60);
      sprintf(btmp,"%ld",hours);
      time_buf += string(btmp) + "h";
    }

    mins = diff_t / (60);
    if (mins > 0) {
      diff_t -= mins * (60);
      sprintf(btmp,"%ld",mins);
      time_buf += string(btmp) + "m";
    }

    secs = diff_t;
    sprintf(btmp,"%ld",secs);
    time_buf += string(btmp) + "s";

    string failure_time;
    if (iter->second.last_failure() == 0) {
      failure_time = string("n/a") + string("\t\t");
    }
    else {
      failure_time = time_buf + string("\t");
    }

    line += space + space + string("Last Ping Failure:  ") + failure_time + "\n";


    //now failure count
    sprintf(btmp, "%ld", iter->second._hresults._failure_count);

    line += space + space + string("# Ping Failure(s):  ") + string(btmp) + "\n\n";

    fputs(line.c_str(),fp);
    ++iter;
  }
  fclose(fp);


  //dump out the application data
  if (_debug) {
    LBData::LBRuleConstIter r_iter = lbdata._lb_rule_coll.begin();
    while (r_iter != lbdata._lb_rule_coll.end()) {
      cout << "squirt out results here." << endl;
      ++r_iter;
    }
  }
}