summaryrefslogtreecommitdiff
path: root/src/lboutput.cc
blob: d682dc233f4820596ca4152de3a794c048e081fd (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
 *
 * **** License ****
 * Version: VPL 1.0
 *
 * The contents of this file are subject to the Vyatta Public License
 * Version 1.0 ("License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.vyatta.com/vpl
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * This code was originally developed by Vyatta, Inc.
 * Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
 * All Rights Reserved.
 *
 * Author: Michael Larson
 * Date: 2007
 * Description:
 *
 * **** End License ****
 *
 */
#include <sys/time.h>
#include <time.h>
#include <syslog.h>

#include <iostream>

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

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

  string wlb_out = _output_path + "/wlb.out";
  string wlb_app_out = _output_path + "/wlb_app.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;
  }

  //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 line("Interface\tStatus\tLast Success\tLast Failure\tNum Failure\n");
  fputs(line.c_str(),fp);
  iter = lbdata._iface_health_coll.begin();

  timeval cur_t;
  gettimeofday(&cur_t,NULL);

  while (iter != lbdata._iface_health_coll.end()) {
    line = string(iter->first) + "\t\t" + string(iter->second._is_active?"active":"failed") + "\t";
    char btmp[256];
    string time_buf;
    
    unsigned long diff_t;
    if (iter->second.last_success() > 0) {
      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";

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

    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";

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

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

    line += "\n";

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


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