Skip to content

Commit

Permalink
Fetched livestatus from downstream.
Browse files Browse the repository at this point in the history
Change-Id: Ic980d4bb0eac54186f675dabdb66c47a8b0184ef
  • Loading branch information
Sven Panne committed Dec 13, 2016
1 parent 7a2f819 commit d809f23
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 46 deletions.
6 changes: 2 additions & 4 deletions livestatus/src/DoubleColumn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
// Boston, MA 02110-1301 USA.

#include "DoubleColumn.h"
#include <cstdio>
#include "DoubleAggregator.h"
#include "DoubleFilter.h"
#include "Renderer.h"

using std::make_unique;
using std::string;
using std::to_string;
using std::unique_ptr;

void DoubleColumn::output(void *row, RowRenderer &r,
Expand All @@ -38,9 +38,7 @@ void DoubleColumn::output(void *row, RowRenderer &r,
}

string DoubleColumn::valueAsString(void *row, contact * /* auth_user */) {
char buf[64];
snprintf(buf, sizeof(buf), "%.10e", getValue(row));
return buf;
return to_string(getValue(row));
}

Filter *DoubleColumn::createFilter(RelationalOperator relOp,
Expand Down
11 changes: 5 additions & 6 deletions livestatus/src/LogCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
#include <dirent.h>
#include <unistd.h>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <ostream>
#include <string>
#include <utility>
#include "Logfile.h"
#include "Logger.h"
#include "MonitoringCore.h"

using std::string;
using std::chrono::system_clock;

#define CHECK_MEM_CYCLE 1000 /* Check memory every N'th new message */
Expand Down Expand Up @@ -97,17 +98,15 @@ void LogCache::updateLogfileIndex() {
DIR *dir = opendir(log_archive_path);

if (dir != nullptr) {
char abspath[4096];
struct dirent *ent, *result;
int len = offsetof(struct dirent, d_name) +
pathconf(log_archive_path, _PC_NAME_MAX) + 1;
ent = static_cast<struct dirent *>(malloc(len));

while (0 == readdir_r(dir, ent, &result) && result != nullptr) {
if (ent->d_name[0] != '.') {
snprintf(abspath, sizeof(abspath), "%s/%s", log_archive_path,
ent->d_name);
scanLogfile(abspath, false);
scanLogfile(string(log_archive_path) + "/" + ent->d_name,
false);
}
// ent = result;
}
Expand All @@ -119,7 +118,7 @@ void LogCache::updateLogfileIndex() {
}
}

void LogCache::scanLogfile(char *path, bool watch) {
void LogCache::scanLogfile(const string &path, bool watch) {
auto logfile = new Logfile(_logger, _commands_holder, path, watch);
time_t since = logfile->since();
if (since != 0) {
Expand Down
3 changes: 2 additions & 1 deletion livestatus/src/LogCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <ctime>
#include <map>
#include <mutex>
#include <string>
#include "nagios.h" // IWYU pragma: keep
class Column;
class CommandsHolder;
Expand Down Expand Up @@ -70,7 +71,7 @@ class LogCache {
bool logCachePreChecks(MonitoringCore *core);

private:
void scanLogfile(char *path, bool watch);
void scanLogfile(const std::string &path, bool watch);
_logfiles_t::iterator findLogfileStartingBefore(time_t);
};

Expand Down
14 changes: 9 additions & 5 deletions livestatus/src/OutputBuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@
#include <sys/select.h>
#include <unistd.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <ostream>
#include <ratio>
#include "ChronoUtils.h"
#include "Logger.h"

using std::chrono::milliseconds;
using std::ostringstream;
using std::setfill;
using std::setw;
using std::string;
using std::to_string;
using std::vector;
Expand Down Expand Up @@ -105,10 +108,11 @@ void OutputBuffer::flush(int fd, int *termination_flag) {
s = _error_message.size();
}

char header[17];
snprintf(header, sizeof(header), "%03u %11d\n",
static_cast<unsigned>(_response_code), s);
writeData(fd, termination_flag, header, 16);
ostringstream os;
os << setw(3) << setfill('0') << static_cast<unsigned>(_response_code)
<< " " << setw(11) << setfill(' ') << s;
string header = os.str();
writeData(fd, termination_flag, header.c_str(), header.size());
writeData(fd, termination_flag, buffer, s);
} else {
writeData(fd, termination_flag, _buffer, size());
Expand Down
6 changes: 2 additions & 4 deletions livestatus/src/PerfdataAggregator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "PerfdataAggregator.h"
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
Expand All @@ -34,6 +33,7 @@
#include "strutil.h"

using std::string;
using std::to_string;
using std::vector;

void PerfdataAggregator::consume(void *row, contact * /* auth_user */,
Expand Down Expand Up @@ -148,14 +148,12 @@ void PerfdataAggregator::output(RowRenderer &r) {
// _operation should beetter be a scoped enumeration.
break;
}
char format[64];
snprintf(format, sizeof(format), "%s=%.8f", entry.first.c_str(), value);
if (first) {
first = false;
} else {
perf_data += " ";
}
perf_data += format;
perf_data += entry.first + "=" + to_string(value);
}
r.output(perf_data);
}
9 changes: 4 additions & 5 deletions livestatus/src/TableContacts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
// Boston, MA 02110-1301 USA.

#include "TableContacts.h"
#include <cstdio>
#include <memory>
#include "AttributeListAsIntColumn.h"
#include "AttributeListColumn.h"
Expand All @@ -39,6 +38,7 @@

using std::make_unique;
using std::string;
using std::to_string;

extern contact *contact_list;

Expand Down Expand Up @@ -79,11 +79,10 @@ void TableContacts::addColumns(Table *table, const string &prefix,
"problems",
reinterpret_cast<char *>(&ctc.service_notification_period) - ref,
indirect_offset));
for (int i = 0; i < MAX_CONTACT_ADDRESSES; i++) {
char b[32];
snprintf(b, sizeof(b), "address%d", i + 1);
for (int i = 1; i <= MAX_CONTACT_ADDRESSES; ++i) {
string b = "address" + to_string(i);
table->addColumn(make_unique<OffsetStringColumn>(
prefix + b, (string("The additional field ") + b),
prefix + b, "The additional field " + b,
reinterpret_cast<char *>(&ctc.address[i]) - ref, indirect_offset));
}

Expand Down
4 changes: 2 additions & 2 deletions livestatus/src/mk_logwatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// Boston, MA 02110-1301 USA.

#include "mk_logwatch.h"
#include <cstdio>
#include <unistd.h>
#include <ostream>
#include "Logger.h"
#include "pnp4nagios.h"
Expand All @@ -41,7 +41,7 @@ void mk_logwatch_acknowledge(Logger *logger, const std::string &logwatch_path,
return;
}
string path = logwatch_path + pnp_cleanup(host_name) + "/" + file_name;
if (remove(path.c_str()) != 0) {
if (unlink(path.c_str()) != 0) {
generic_error ge("Cannot acknowledge mk_logfile file '" + file_name +
"' of host '" + host_name + "'");
Warning(logger) << ge;
Expand Down
42 changes: 23 additions & 19 deletions livestatus/src/unixcat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
#include <sys/un.h>
#include <unistd.h>
#include <cerrno>
#include <cinttypes>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>

using std::cerr;
using std::endl;
using std::string;
using std::to_string;

int copy_data(int from, int to);
void *voidp;
Expand All @@ -47,6 +51,10 @@ struct thread_info {
int terminate_on_read_eof;
};

void printErrno(const string &msg) {
cerr << msg + ": " + strerror(errno) << endl;
}

ssize_t read_with_timeout(int from, char *buffer, int size, int us) {
fd_set fds;
FD_ZERO(&fds);
Expand Down Expand Up @@ -74,8 +82,7 @@ void *copy_thread(void *info) {
ssize_t r =
read_with_timeout(from, read_buffer, sizeof(read_buffer), 1000000);
if (r == -1) {
fprintf(stderr, "Error reading from %d: %s\n", from,
strerror(errno));
printErrno("Error reading from " + to_string(from));
break;
} else if (r == 0) {
if (ti->should_shutdown != 0) {
Expand All @@ -95,10 +102,8 @@ void *copy_thread(void *info) {
while (bytes_to_write > 0) {
ssize_t bytes_written = write(to, buffer, bytes_to_write);
if (bytes_written == -1) {
uintmax_t b = bytes_to_write;
fprintf(stderr,
"Error: Cannot write %" PRIuMAX " bytes to %d: %s\n", b,
to, strerror(errno));
printErrno("Error: Cannot write " + to_string(bytes_to_write) +
" bytes to " + to_string(to));
break;
}
buffer += bytes_written;
Expand All @@ -110,35 +115,34 @@ void *copy_thread(void *info) {

int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s UNIX-socket\n", argv[0]);
cerr << "Usage: " << argv[0] << " UNIX-socket" << endl;
exit(1);
}

// https://llvm.org/bugs/show_bug.cgi?id=29089
signal(SIGWINCH, SIG_IGN); // NOLINT

const char *unixpath = argv[1];
string unixpath = argv[1];
struct stat st;

if (0 != stat(unixpath, &st)) {
fprintf(stderr, "No UNIX socket %s existing\n", unixpath);
if (0 != stat(unixpath.c_str(), &st)) {
cerr << "No UNIX socket " << unixpath << " existing" << endl;
exit(2);
}

int sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr, "Cannot create client socket: %s\n", strerror(errno));
printErrno("Cannot create client socket");
exit(3);
}

/* Connect */
struct sockaddr_un sockaddr;
sockaddr.sun_family = AF_UNIX;
strncpy(sockaddr.sun_path, unixpath, sizeof(sockaddr.sun_path));
strncpy(sockaddr.sun_path, unixpath.c_str(), sizeof(sockaddr.sun_path));
if (connect(sock, reinterpret_cast<struct sockaddr *>(&sockaddr),
sizeof(sockaddr)) != 0) {
fprintf(stderr, "Couldn't connect to UNIX-socket at %s: %s.\n",
unixpath, strerror(errno));
printErrno("Couldn't connect to UNIX-socket at " + unixpath);
close(sock);
exit(4);
}
Expand All @@ -150,13 +154,13 @@ int main(int argc, char **argv) {
0 ||
pthread_create(&toleft_thread, nullptr, copy_thread, &toleft_info) !=
0) {
fprintf(stderr, "Couldn't create threads: %s.\n", strerror(errno));
printErrno("Couldn't create threads");
close(sock);
exit(5);
}
if (pthread_join(toleft_thread, nullptr) != 0 ||
pthread_join(toright_thread, nullptr) != 0) {
fprintf(stderr, "Couldn't join threads: %s.\n", strerror(errno));
printErrno("Couldn't join threads");
close(sock);
exit(6);
}
Expand Down

0 comments on commit d809f23

Please sign in to comment.