initial commit

This commit is contained in:
Phillip Smith
2021-08-24 17:55:37 +10:00
commit 69e42aac8d
387 changed files with 69168 additions and 0 deletions

87
api/c++/Livestatus.cc Normal file
View File

@@ -0,0 +1,87 @@
// +------------------------------------------------------------------+
// | ____ _ _ __ __ _ __ |
// | / ___| |__ ___ ___| | __ | \/ | |/ / |
// | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
// | | |___| | | | __/ (__| < | | | | . \ |
// | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
// | |
// | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
// +------------------------------------------------------------------+
//
// This file is part of Check_MK.
// The official homepage is at http://mathias-kettner.de/check_mk.
//
// check_mk is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation in version 2. check_mk is distributed
// in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
// out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more de-
// ails. You should have received a copy of the GNU General Public
// License along with GNU Make; see the file COPYING. If not, write
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.
#include "Livestatus.h"
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
void Livestatus::connectUNIX(const char *socket_path) {
_connection = socket(PF_LOCAL, SOCK_STREAM, 0);
struct sockaddr_un sockaddr;
sockaddr.sun_family = AF_UNIX;
strncpy(sockaddr.sun_path, socket_path, sizeof(sockaddr.sun_path) - 1);
sockaddr.sun_path[sizeof(sockaddr.sun_path) - 1] = '\0';
if (0 > connect(_connection, (const struct sockaddr *)&sockaddr,
sizeof(sockaddr))) {
close(_connection);
_connection = -1;
} else
_file = fdopen(_connection, "r");
}
Livestatus::~Livestatus() { disconnect(); }
void Livestatus::disconnect() {
if (isConnected()) {
if (_file)
fclose(_file);
else
close(_connection);
}
_connection = -1;
_file = 0;
}
void Livestatus::sendQuery(const char *query) {
write(_connection, query, strlen(query));
std::string separators = "Separators: 10 1 2 3\n";
write(_connection, separators.c_str(), separators.size());
shutdown(_connection, SHUT_WR);
}
std::vector<std::string> *Livestatus::nextRow() {
char line[65536];
if (0 != fgets(line, sizeof(line), _file)) {
// strip trailing linefeed
char *end = strlen(line) + line;
if (end > line && *(end - 1) == '\n') {
*(end - 1) = 0;
--end;
}
std::vector<std::string> *row = new std::vector<std::string>;
char *scan = line;
while (scan < end) {
char *zero = scan;
while (zero < end && *zero != '\001') zero++;
*zero = 0;
row->push_back(std::string(scan));
scan = zero + 1;
}
return row;
} else
return 0;
}

50
api/c++/Livestatus.h Normal file
View File

@@ -0,0 +1,50 @@
// +------------------------------------------------------------------+
// | ____ _ _ __ __ _ __ |
// | / ___| |__ ___ ___| | __ | \/ | |/ / |
// | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
// | | |___| | | | __/ (__| < | | | | . \ |
// | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
// | |
// | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
// +------------------------------------------------------------------+
//
// This file is part of Check_MK.
// The official homepage is at http://mathias-kettner.de/check_mk.
//
// check_mk is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation in version 2. check_mk is distributed
// in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
// out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more de-
// ails. You should have received a copy of the GNU General Public
// License along with GNU Make; see the file COPYING. If not, write
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.
#ifndef Livestatus_h
#define Livestatus_h
#include <stdio.h>
#include <string>
#include <vector>
// simple C++ API for accessing Livestatus from C++,
// currently supports only UNIX sockets, no TCP. But
// this is only a simple enhancement.
class Livestatus {
int _connection;
FILE *_file;
public:
Livestatus() : _connection(-1), _file(0){};
~Livestatus();
void connectUNIX(const char *socketpath);
bool isConnected() const { return _connection >= 0; };
void disconnect();
void sendQuery(const char *query);
std::vector<std::string> *nextRow();
};
#endif // Livestatus_h

42
api/c++/Makefile Normal file
View File

@@ -0,0 +1,42 @@
# +------------------------------------------------------------------+
# | ____ _ _ __ __ _ __ |
# | / ___| |__ ___ ___| | __ | \/ | |/ / |
# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
# | | |___| | | | __/ (__| < | | | | . \ |
# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
# | |
# | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation in version 2. check_mk is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more de-
# tails. You should have received a copy of the GNU General Public
# License along with GNU Make; see the file COPYING. If not, write
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
ifneq (DEBUG,)
CXXFLAGS += -g -DDEBUG
LDFLAGS += -g
endif
all: demo
demo.o: demo.cc Livestatus.h
$(CXX) $(CXXFLAGS) -c -o $@ $<
Livestatus.o: Livestatus.cc Livestatus.h
$(CXX) $(CXXFLAGS) -c -o $@ $<
demo: demo.o Livestatus.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^
clean:
$(RM) demo.o demo Livetatus.o

54
api/c++/demo.cc Normal file
View File

@@ -0,0 +1,54 @@
// +------------------------------------------------------------------+
// | ____ _ _ __ __ _ __ |
// | / ___| |__ ___ ___| | __ | \/ | |/ / |
// | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
// | | |___| | | | __/ (__| < | | | | . \ |
// | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
// | |
// | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
// +------------------------------------------------------------------+
//
// This file is part of Check_MK.
// The official homepage is at http://mathias-kettner.de/check_mk.
//
// check_mk is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation in version 2. check_mk is distributed
// in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
// out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more de-
// ails. You should have received a copy of the GNU General Public
// License along with GNU Make; see the file COPYING. If not, write
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.
#include <stdio.h>
#include "Livestatus.h"
const char *query =
"GET status\nColumns: livestatus_version program_version\nColumnHeaders: on\n";
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s SOCKETPATH\n", argv[0]);
return 1;
}
const char *socket_path = argv[1];
Livestatus live;
live.connectUNIX(socket_path);
if (live.isConnected()) {
fprintf(stderr, "Couldn't connect to socket '%s'\n", socket_path);
return 1;
}
live.sendQuery(query);
std::vector<std::string> *row;
while (0 != (row = live.nextRow())) {
printf("Line:\n");
for (size_t i = 0; i < row->size(); i++)
printf("%s\n", (*row)[i].c_str());
delete row;
}
live.disconnect();
return 0;
}