worker.cc 3.74 KB
Newer Older
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * Copyright 2011 - 2019 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 *
 */
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
19

David Boucher's avatar
David Boucher committed
20
21
22
23
24
#include "com/centreon/broker/stats/worker.hh"
#include <fcntl.h>
#include <poll.h>
#include <time.h>
#include <unistd.h>
25
#include <cerrno>
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
26
27
#include <iomanip>
#include <sstream>
28
#include "com/centreon/broker/config/applier/endpoint.hh"
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
29
#include "com/centreon/broker/config/applier/modules.hh"
30
#include "com/centreon/broker/log_v2.hh"
31
#include "com/centreon/broker/stats/builder.hh"
32
#include "com/centreon/exceptions/msg_fmt.hh"
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
33

34
using namespace com::centreon::exceptions;
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
35
36
37
38
39
40
41
42
43
44
using namespace com::centreon::broker::stats;

/**
 *  Default constructor.
 */
worker::worker() : _fd(-1) {}

/**
 *  Destructor.
 */
David Boucher's avatar
David Boucher committed
45
worker::~worker() noexcept {
David Boucher's avatar
David Boucher committed
46
  _exit = true;
David Boucher's avatar
David Boucher committed
47
  _thread.join();
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
48
49
50
51
52
53
}

/**
 *  Run the statistics thread.
 *
 *  @param[in] fifo_file Path to the FIFO file.
54
 *  @param[in] type     The type of this FIFO.
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
55
 */
David Boucher's avatar
David Boucher committed
56
void worker::run(std::string const& fifo_file) {
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
57
58
59
60
  // Close FD.
  _close();

  // Set FIFO file.
David Boucher's avatar
David Boucher committed
61
  _fifo = fifo_file;
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
62
63

  // Set exit flag.
David Boucher's avatar
David Boucher committed
64
  _exit = false;
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
65
66

  // Launch thread.
David Boucher's avatar
David Boucher committed
67
  _thread = std::thread(&worker::_run, this);
68
  pthread_setname_np(_thread.native_handle(), "stats_worker");
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
}

/**
 *  Close FIFO fd.
 */
void worker::_close() {
  if (_fd >= 0) {
    close(_fd);
    _fd = -1;
  }
}

/**
 *  Open FIFO.
83
84
 *
 *  @return true on success.
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
85
 */
86
87
bool worker::_open() {
  bool retval;
Dorian Guillois's avatar
Dorian Guillois committed
88
  _fd = open(_fifo.c_str(), O_WRONLY | O_NONBLOCK);
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
89
  if (_fd < 0) {
90
91
    if (errno != ENXIO) {
      char const* msg(strerror(errno));
92
      throw msg_fmt("cannot open FIFO file: {}", msg);
David Boucher's avatar
David Boucher committed
93
    } else
94
      retval = false;
David Boucher's avatar
David Boucher committed
95
  } else
96
    retval = true;
97
  return retval;
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
98
99
100
101
102
}

/**
 *  Thread entry point.
 */
David Boucher's avatar
David Boucher committed
103
void worker::_run() {
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
104
  try {
David Boucher's avatar
David Boucher committed
105
    while (!_exit) {
106
107
      // Check file opening.
      if (_buffer.empty()) {
108
        usleep(100000);
109
        if (!_open())
David Boucher's avatar
David Boucher committed
110
          continue;
111
112
      }

Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
113
      // FD sets.
114
115
116
117
      pollfd fds;
      fds.fd = _fd;
      fds.events = POLLOUT;
      fds.revents = 0;
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
118
119

      // Multiplexing.
120
      int flagged = poll(&fds, 1, 1000);
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
121
122
123
124
125
126

      // Error.
      if (flagged < 0) {
        // Unrecoverable.
        if (errno != EINTR) {
          char const* msg(strerror(errno));
127
          throw msg_fmt("multiplexing failure: {}", msg);
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
128
        }
David Boucher's avatar
David Boucher committed
129
      } else if (flagged > 0) {
130
131
        // FD error.
        if ((fds.revents & (POLLERR | POLLNVAL | POLLHUP)))
132
          throw msg_fmt("FIFO fd has pending error");
133
134
        // Readable.
        else if ((fds.revents & POLLOUT)) {
135
          if (_buffer.empty()) {
136
            // Generate statistics.
137
            builder stats_builder;
138
            stats_builder.build();
139
140
            _buffer = stats_builder.data();
          }
141
142

          // Write data.
143
144
145
146
147
148
          ssize_t delta = 0;
          while (delta < _buffer.size()) {
            delta += write(_fd, _buffer.c_str() + delta, _buffer.size() - delta);
          }
          _close();
          _buffer.clear();
149
        }
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
150
151
      }
    }
David Boucher's avatar
David Boucher committed
152
  } catch (std::exception const& e) {
153
154
155
    log_v2::stats()->error(
        "stats: FIFO thread will exit due to the following error: {}",
        e.what());
David Boucher's avatar
David Boucher committed
156
  } catch (...) {
157
158
    log_v2::stats()->error(
        "stats: FIFO thread will exit due to an unknown error");
Mathieu Kermagoret's avatar
Mathieu Kermagoret committed
159
  }
Dorian Guillois's avatar
Dorian Guillois committed
160
  ::unlink(_fifo.c_str());
David Boucher's avatar
David Boucher committed
161
}