42 #include <sys/types.h>
55 "# Persistent Status Log for %s\n"
56 "# The last entry in this file will be returned during the %s\n"
57 "# status check. Once the issues described below have been dealt with, this file\n"
58 "# can be removed to clear the %s persistent status and\n"
59 "# return to normal dynamic status checking.\n"
61 string header_string() {
83 std::vector<std::string> additional_lines) {
84 lock_guard<mutex> lock(ms_mutex);
93 text = header_string();
101 ms_status.get(&code, status_text);
109 timeinfo = localtime(&rawtime);
110 strftime(buffer, 80,
"%Y-%m-%d %H:%M:%S", timeinfo);
111 text +=
format(
"- Time %s\n", (
const char *)buffer);
114 for (
auto &
str : additional_lines) {
120 int fd = ::open(ms_fname.c_str(), O_WRONLY | O_APPEND);
122 string message = strerror(errno);
123 HT_ERRORF(
"Unable to open file \"%s\" for writing - %s", ms_fname.c_str(),
136 lock_guard<mutex> lock(ms_mutex);
137 if (ms_fname.empty())
145 string last_line, contents;
147 const char *base = contents.c_str();
148 const char *ptr = strchr(base,
'\n');
150 if (*base !=
'#' && *base !=
'-')
151 last_line = string(base, ptr-base);
153 ptr = strchr(base,
'\n');
155 if (*base && *base !=
'#' && *base !=
'-')
156 last_line = string(base, ptr-base);
157 if (!last_line.empty()) {
158 if (last_line.find(
"WARNING - ") == 0)
160 else if (last_line.find(
"CRITICAL - ") == 0)
162 else if (last_line.find(
"UNKNOWN - ") == 0)
164 else if (last_line.find(
"OK - ") != 0)
165 HT_INFOF(
"Corrupt persistent status file %s", ms_fname.c_str());
Retrieves system information (hardware, installation directory, etc)
void initialize(const String &name)
Public initialization function - creates a singleton instance of LogWriter.
Holds Nagios-style program status information.
static bool read(const String &fname, String &contents)
Reads a whole file into a String.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Po::typed_value< String > * str(String *v=0)
static ssize_t write(const String &fname, const std::string &contents)
Writes a String buffer to a file; the file is overwritten if it already exists.
static bool exists(const String &fname)
Checks if a file or directory exists.
Code
Enumeration for status codes.
static const char * code_to_string(Code code)
File system utility functions.
static Status ms_status
Current persistent status.
bool status(ContextPtr &context, Timer &timer, Status &status)
Runs a status check on the master.
static String exe_name
The exe file name.
Logging routines and macros.
static std::mutex ms_mutex
Mutex for serializaing access to members
Compatibility Macros for C/C++.
static void set(const Status &status, std::vector< std::string > additional_lines)
Sets persistent status.
static void get(Status &status)
Gets persistent status.
#define HT_INFOF(msg,...)
static String install_dir
The installation directory.
static void initialize()
Initializes variables.
Declarations for StatusPersister.
#define HT_ERRORF(msg,...)
static std::string ms_fname
Name of persistent status file.