From df19d8f8ddc8f19568b8e239d7f6d0b3ba35fbaa Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 14:49:54 +0200 Subject: [PATCH 01/78] include unix plugin --- src/evcollect/Makefile.am | 2 + src/evcollect/evcollectd.cc | 13 +++++ src/evcollect/plugins/unix/unix_plugin.cc | 65 +++++++++++++++++++++++ src/evcollect/plugins/unix/unix_plugin.h | 47 ++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/evcollect/plugins/unix/unix_plugin.cc create mode 100644 src/evcollect/plugins/unix/unix_plugin.h diff --git a/src/evcollect/Makefile.am b/src/evcollect/Makefile.am index 9411e08..fec52a0 100644 --- a/src/evcollect/Makefile.am +++ b/src/evcollect/Makefile.am @@ -60,6 +60,8 @@ evcollectd_SOURCES = \ plugins/hostname/hostname_plugin.cc \ plugins/logfile/logfile_plugin.h \ plugins/logfile/logfile_plugin.cc \ + plugins/unix/unix_plugin.h \ + plugins/unix/unix_plugin.cc \ plugin.h \ plugin.cc \ plugin_map.h \ diff --git a/src/evcollect/evcollectd.cc b/src/evcollect/evcollectd.cc index 86077c9..bcd5969 100644 --- a/src/evcollect/evcollectd.cc +++ b/src/evcollect/evcollectd.cc @@ -37,6 +37,7 @@ #include #include #include +#include using namespace evcollect; @@ -186,6 +187,15 @@ int main(int argc, const char** argv) { s.properties.properties.emplace_back( std::make_pair("regex", "(?[^\|]*)?(?.*)")); } + { + conf.event_bindings.emplace_back(); + auto& b = conf.event_bindings.back(); + b.event_name = "sys.unix"; + b.interval_micros = 1000000; + b.sources.emplace_back(); + auto& s = b.sources.back(); + s.plugin_name = "unix"; + } /* load plugins */ std::unique_ptr plugin_map(new PluginMap()); @@ -195,6 +205,9 @@ int main(int argc, const char** argv) { plugin_map->registerSourcePlugin( "logfile", std::unique_ptr(new plugin_logfile::LogfileSourcePlugin())); + plugin_map->registerSourcePlugin( + "unix", + std::unique_ptr(new plugin_unix::UnixPlugin())); /* initialize event bindings */ auto rc = ReturnCode::success(); diff --git a/src/evcollect/plugins/unix/unix_plugin.cc b/src/evcollect/plugins/unix/unix_plugin.cc new file mode 100644 index 0000000..ab200a2 --- /dev/null +++ b/src/evcollect/plugins/unix/unix_plugin.cc @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ +#include +#include +#include +#include +#include "unix_plugin.h" + +namespace evcollect { +namespace plugin_unix { + +ReturnCode UnixPlugin::pluginGetNextEvent( + void* userdata, + std::string* event_json) { + std::string hostname; + std::string hostname_fqdn; + + hostname.resize(1024); + if (gethostname(&hostname[0], hostname.size()) == -1) { + return ReturnCode::error("SYSCALL_FAILED", "gethostname() failed"); + } else { + hostname.resize(strlen(hostname.data())); + } + + struct hostent* h = gethostbyname(hostname.c_str()); + hostname_fqdn = std::string(h->h_name); + + *event_json = StringUtil::format( + R"({ "test": "$0", "blah": "$1" })", + StringUtil::jsonEscape(hostname), + StringUtil::jsonEscape(hostname_fqdn)); + + return ReturnCode::success(); +} + +bool UnixPlugin::pluginHasPendingEvent( + void* userdata) { + return false; +} + +} // namespace plugin_unix +} // namespace evcollect + diff --git a/src/evcollect/plugins/unix/unix_plugin.h b/src/evcollect/plugins/unix/unix_plugin.h new file mode 100644 index 0000000..ebf8495 --- /dev/null +++ b/src/evcollect/plugins/unix/unix_plugin.h @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ +#pragma once +#include +#include +#include + +namespace evcollect { +namespace plugin_unix { + +class UnixPlugin : public SourcePlugin { +public: + + ReturnCode pluginGetNextEvent( + void* userdata, + std::string* event_json) override; + + bool pluginHasPendingEvent( + void* userdata) override; + +}; + +} // namespace plugin_unix +} // namespace evcollect + From 8301837ce3c6cbe11f76ae11130ddb352b72d520 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 14:56:25 +0200 Subject: [PATCH 02/78] rename UnixPlugin -> UnixStatsPlugin --- src/evcollect/Makefile.am | 4 ++-- src/evcollect/evcollectd.cc | 8 ++++---- .../unix_stats_plugin.cc} | 18 +++++------------- .../unix_stats_plugin.h} | 6 +++--- 4 files changed, 14 insertions(+), 22 deletions(-) rename src/evcollect/plugins/{unix/unix_plugin.cc => unix_stats/unix_stats_plugin.cc} (77%) rename src/evcollect/plugins/{unix/unix_plugin.h => unix_stats/unix_stats_plugin.h} (93%) diff --git a/src/evcollect/Makefile.am b/src/evcollect/Makefile.am index fec52a0..6d7eacd 100644 --- a/src/evcollect/Makefile.am +++ b/src/evcollect/Makefile.am @@ -60,8 +60,8 @@ evcollectd_SOURCES = \ plugins/hostname/hostname_plugin.cc \ plugins/logfile/logfile_plugin.h \ plugins/logfile/logfile_plugin.cc \ - plugins/unix/unix_plugin.h \ - plugins/unix/unix_plugin.cc \ + plugins/unix_stats/unix_stats_plugin.h \ + plugins/unix_stats/unix_stats_plugin.cc \ plugin.h \ plugin.cc \ plugin_map.h \ diff --git a/src/evcollect/evcollectd.cc b/src/evcollect/evcollectd.cc index bcd5969..5d4b931 100644 --- a/src/evcollect/evcollectd.cc +++ b/src/evcollect/evcollectd.cc @@ -37,7 +37,7 @@ #include #include #include -#include +#include using namespace evcollect; @@ -194,7 +194,7 @@ int main(int argc, const char** argv) { b.interval_micros = 1000000; b.sources.emplace_back(); auto& s = b.sources.back(); - s.plugin_name = "unix"; + s.plugin_name = "unix_stats"; } /* load plugins */ @@ -206,8 +206,8 @@ int main(int argc, const char** argv) { "logfile", std::unique_ptr(new plugin_logfile::LogfileSourcePlugin())); plugin_map->registerSourcePlugin( - "unix", - std::unique_ptr(new plugin_unix::UnixPlugin())); + "unix_stats", + std::unique_ptr(new plugin_unix_stats::UnixStatsPlugin())); /* initialize event bindings */ auto rc = ReturnCode::success(); diff --git a/src/evcollect/plugins/unix/unix_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc similarity index 77% rename from src/evcollect/plugins/unix/unix_plugin.cc rename to src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index ab200a2..ad99b27 100644 --- a/src/evcollect/plugins/unix/unix_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -26,26 +26,18 @@ #include #include #include -#include "unix_plugin.h" +#include "unix_stats_plugin.h" namespace evcollect { -namespace plugin_unix { +namespace plugin_unix_stats { -ReturnCode UnixPlugin::pluginGetNextEvent( +ReturnCode UnixStatsPlugin::pluginGetNextEvent( void* userdata, std::string* event_json) { std::string hostname; std::string hostname_fqdn; - hostname.resize(1024); - if (gethostname(&hostname[0], hostname.size()) == -1) { - return ReturnCode::error("SYSCALL_FAILED", "gethostname() failed"); - } else { - hostname.resize(strlen(hostname.data())); - } - struct hostent* h = gethostbyname(hostname.c_str()); - hostname_fqdn = std::string(h->h_name); *event_json = StringUtil::format( R"({ "test": "$0", "blah": "$1" })", @@ -55,11 +47,11 @@ ReturnCode UnixPlugin::pluginGetNextEvent( return ReturnCode::success(); } -bool UnixPlugin::pluginHasPendingEvent( +bool UnixStatsPlugin::pluginHasPendingEvent( void* userdata) { return false; } -} // namespace plugin_unix +} // namespace plugin_unix_stats } // namespace evcollect diff --git a/src/evcollect/plugins/unix/unix_plugin.h b/src/evcollect/plugins/unix_stats/unix_stats_plugin.h similarity index 93% rename from src/evcollect/plugins/unix/unix_plugin.h rename to src/evcollect/plugins/unix_stats/unix_stats_plugin.h index ebf8495..321a7ed 100644 --- a/src/evcollect/plugins/unix/unix_plugin.h +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.h @@ -28,9 +28,9 @@ #include namespace evcollect { -namespace plugin_unix { +namespace plugin_unix_stats { -class UnixPlugin : public SourcePlugin { +class UnixStatsPlugin : public SourcePlugin { public: ReturnCode pluginGetNextEvent( @@ -42,6 +42,6 @@ class UnixPlugin : public SourcePlugin { }; -} // namespace plugin_unix +} // namespace plugin_unix_stats } // namespace evcollect From 1367251fa22684d85406947fc74c04c9bccc3573 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 15:11:00 +0200 Subject: [PATCH 03/78] UnixStatsPlugin::getMountInfo stub --- .../plugins/unix_stats/unix_stats_plugin.cc | 12 ++++++++++++ .../plugins/unix_stats/unix_stats_plugin.h | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index ad99b27..d030cda 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -52,6 +52,18 @@ bool UnixStatsPlugin::pluginHasPendingEvent( return false; } +std::vector UnixStatsPlugin::getMountInfo() { + UnixStatsPlugin::MountInfo mount_info = { + .device = "test", + .mount_point = "test", + .fs_type = UnixStatsPlugin::fsType::EXT3 + }; + + std::vector infos; + infos.emplace_back(mount_info); + return infos; +} + } // namespace plugin_unix_stats } // namespace evcollect diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.h b/src/evcollect/plugins/unix_stats/unix_stats_plugin.h index 321a7ed..23bbf1e 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.h +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.h @@ -33,6 +33,19 @@ namespace plugin_unix_stats { class UnixStatsPlugin : public SourcePlugin { public: + enum fsType { + PROC, + SWAP, + EXT3, + EXT4 + }; + + struct MountInfo { + std::string device; + std::string mount_point; + fsType fs_type; + }; + ReturnCode pluginGetNextEvent( void* userdata, std::string* event_json) override; @@ -40,6 +53,11 @@ class UnixStatsPlugin : public SourcePlugin { bool pluginHasPendingEvent( void* userdata) override; + +protected: + + std::vector getMountInfo(); + }; } // namespace plugin_unix_stats From b34262ee970c722d6528125375e437a91f02af82 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 15:43:08 +0200 Subject: [PATCH 04/78] get mount info for apple --- .../plugins/unix_stats/unix_stats_plugin.cc | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index d030cda..b929a4a 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -24,10 +24,19 @@ */ #include #include +#include #include #include #include "unix_stats_plugin.h" +#ifdef linux +#include +#elif __APPLE__ +#include +#include +#include +#endif + namespace evcollect { namespace plugin_unix_stats { @@ -37,13 +46,13 @@ ReturnCode UnixStatsPlugin::pluginGetNextEvent( std::string hostname; std::string hostname_fqdn; + auto mount_info = getMountInfo(); *event_json = StringUtil::format( R"({ "test": "$0", "blah": "$1" })", StringUtil::jsonEscape(hostname), StringUtil::jsonEscape(hostname_fqdn)); - return ReturnCode::success(); } @@ -53,15 +62,27 @@ bool UnixStatsPlugin::pluginHasPendingEvent( } std::vector UnixStatsPlugin::getMountInfo() { - UnixStatsPlugin::MountInfo mount_info = { - .device = "test", - .mount_point = "test", - .fs_type = UnixStatsPlugin::fsType::EXT3 - }; - - std::vector infos; - infos.emplace_back(mount_info); - return infos; +std::vector mount_info; + +#ifdef linux + auto mntent = getmntent("/etc/fstab"); + +#elif __APPLE__ + + struct statfs *mntbuf; + auto mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); + for (size_t i = 0; i < mntsize; ++i) { + UnixStatsPlugin::MountInfo mn_info = { + .device = mntbuf[i].f_mntfromname, + .mount_point = mntbuf[i].f_mntonname //FIXME add type + }; + + mount_info.emplace_back(mn_info); + } + +#endif + + return mount_info; } } // namespace plugin_unix_stats From f080a295b43e9db7fcb9a6eca0ea7295acb249c1 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 15:57:35 +0200 Subject: [PATCH 05/78] mount info for linux --- src/evcollect/plugins/unix_stats/unix_stats_plugin.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index b929a4a..7749c25 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -65,7 +65,9 @@ std::vector UnixStatsPlugin::getMountInfo() { std::vector mount_info; #ifdef linux - auto mntent = getmntent("/etc/fstab"); + auto file = setmentent("/etc/fstab", "r"); + auto mntent = getmntent(file); + #elif __APPLE__ From 7eba3fc7ae7bbc8c883990ed710fd15b4fa18ec2 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 16:21:21 +0200 Subject: [PATCH 06/78] fix linux build --- src/evcollect/util/logging.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/evcollect/util/logging.cc b/src/evcollect/util/logging.cc index a5e377a..070d1e3 100644 --- a/src/evcollect/util/logging.cc +++ b/src/evcollect/util/logging.cc @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef HAVE_SYSLOG_H #include #endif From e8f1027412ae02c13eae90068a04515c7589e32c Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 16:25:14 +0200 Subject: [PATCH 07/78] fix linux build --- src/evcollect/util/return_code.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/evcollect/util/return_code.h b/src/evcollect/util/return_code.h index 4b5cc80..1e82a6a 100644 --- a/src/evcollect/util/return_code.h +++ b/src/evcollect/util/return_code.h @@ -26,6 +26,7 @@ #include #include #include +#include class ReturnCode { public: From 636f25c3b50e5ec9327a0d6b2468a3441ffa39a7 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 16:27:49 +0200 Subject: [PATCH 08/78] fix linux build --- src/evcollect/util/time.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/evcollect/util/time.cc b/src/evcollect/util/time.cc index c2e54fd..053f48e 100644 --- a/src/evcollect/util/time.cc +++ b/src/evcollect/util/time.cc @@ -33,6 +33,7 @@ #endif #include "time.h" #include "stringutil.h" +#include "logging.h" UnixTime WallClock::now() { return UnixTime(WallClock::getUnixMicros()); From 07e667b073ef908dcd610e82942ad27a5cef6ae1 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 16:29:39 +0200 Subject: [PATCH 09/78] fix linux build --- src/evcollect/plugins/eventql/eventql_plugin.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/evcollect/plugins/eventql/eventql_plugin.cc b/src/evcollect/plugins/eventql/eventql_plugin.cc index 26764cb..c04afe6 100644 --- a/src/evcollect/plugins/eventql/eventql_plugin.cc +++ b/src/evcollect/plugins/eventql/eventql_plugin.cc @@ -23,6 +23,7 @@ */ #include #include +#include #include #include From 596ed5549863021c0ebb63ab4b0882385bd247bb Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 16:38:57 +0200 Subject: [PATCH 10/78] fix linux build --- src/evcollect/util/sha1.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/evcollect/util/sha1.cc b/src/evcollect/util/sha1.cc index 4b88fed..f037b93 100644 --- a/src/evcollect/util/sha1.cc +++ b/src/evcollect/util/sha1.cc @@ -22,6 +22,8 @@ * code of your own applications */ #include +#include +#include #include "sha1.h" #include "stringutil.h" From d02c4453f6da4ff09988e985a6e393ecc3d86e80 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 16:52:53 +0200 Subject: [PATCH 11/78] fix linker flags for gcc --- src/evcollect/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/evcollect/Makefile.am b/src/evcollect/Makefile.am index 8bc497a..5a47e63 100644 --- a/src/evcollect/Makefile.am +++ b/src/evcollect/Makefile.am @@ -81,6 +81,6 @@ evcollectd_SOURCES = \ evcollect.cc \ evcollectd.cc -evcollectd_LDFLAGS = $(PCRE_LDFLAGS_) $(CURL_LDFLAGS_) $(PTHREAD_LDFLAGS_) +evcollectd_LDADD = $(PCRE_LDFLAGS_) $(CURL_LDFLAGS_) $(PTHREAD_LDFLAGS_) bin_PROGRAMS += evcollectd From b9866026ed547b3b494de5b9c19fc987ca28903a Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 17:28:26 +0200 Subject: [PATCH 12/78] wip --- .../plugins/unix_stats/unix_stats_plugin.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index 7749c25..7f8cf2c 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -46,8 +46,21 @@ ReturnCode UnixStatsPlugin::pluginGetNextEvent( std::string hostname; std::string hostname_fqdn; - auto mount_info = getMountInfo(); + //event_json->append("(["); + //auto mount_info = getMountInfo(); + //for (size_t i = 0; i < mount_info.size(); ++i) { + // if (i > 0) { + // event_json->append(","); + // } + + // //event_json->append( + // // R"({ "test": "$0", "blah": "$1" })", + // // StringUtil::jsonEscape(mount_info[i].device), + // // StringUtil::jsonEscape(mount_info[i].mount_point)); + //} + + //event_json->append("])"); *event_json = StringUtil::format( R"({ "test": "$0", "blah": "$1" })", From 98f88aa5252f41c06b584dca4777c541c456680e Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 18:10:23 +0200 Subject: [PATCH 13/78] UnixStatsPlugin: ifree disk space --- .../plugins/unix_stats/unix_stats_plugin.cc | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index 7f8cf2c..ad670e3 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -46,26 +46,29 @@ ReturnCode UnixStatsPlugin::pluginGetNextEvent( std::string hostname; std::string hostname_fqdn; - //event_json->append("(["); - - //auto mount_info = getMountInfo(); - //for (size_t i = 0; i < mount_info.size(); ++i) { - // if (i > 0) { - // event_json->append(","); - // } - - // //event_json->append( - // // R"({ "test": "$0", "blah": "$1" })", - // // StringUtil::jsonEscape(mount_info[i].device), - // // StringUtil::jsonEscape(mount_info[i].mount_point)); - //} - - //event_json->append("])"); - - *event_json = StringUtil::format( - R"({ "test": "$0", "blah": "$1" })", - StringUtil::jsonEscape(hostname), - StringUtil::jsonEscape(hostname_fqdn)); + event_json->append("(["); + + auto mount_info = getMountInfo(); + for (size_t i = 0; i < mount_info.size(); ++i) { + if (i > 0) { + event_json->append(","); + } + + struct statvfs buf; + if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { + printf("statvfs failed /n"); + continue; + } + + event_json->append(StringUtil::format( + R"({ "filesystem": "$0", "ifree": $1, "mount_point": "$2" })", + StringUtil::jsonEscape(mount_info[i].device), + buf.f_bsize * buf.f_bavail / (1024 * 1024), + StringUtil::jsonEscape(mount_info[i].mount_point))); + } + + event_json->append("])"); + return ReturnCode::success(); } From cbf67acf56eea3fb38cc9de1de54b0481facb439 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 18:10:43 +0200 Subject: [PATCH 14/78] hardcoded config with unix_stats --- src/evcollect/config.cc | 9 +++++++++ src/evcollect/evcollectd.cc | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/evcollect/config.cc b/src/evcollect/config.cc index b0a17fa..50e71e6 100644 --- a/src/evcollect/config.cc +++ b/src/evcollect/config.cc @@ -79,6 +79,15 @@ ReturnCode loadConfig( std::vector { "sys.alive", "test/sys.alive.rollup" })); } + { + conf->event_bindings.emplace_back(); + auto& b = conf->event_bindings.back(); + b.event_name = "sys.unix"; + b.interval_micros = 1000000; + b.sources.emplace_back(); + auto& s = b.sources.back(); + s.plugin_name = "unix_stats"; + } return ReturnCode::success(); } diff --git a/src/evcollect/evcollectd.cc b/src/evcollect/evcollectd.cc index 4cfab55..84cca32 100644 --- a/src/evcollect/evcollectd.cc +++ b/src/evcollect/evcollectd.cc @@ -200,15 +200,6 @@ int main(int argc, const char** argv) { logFatal("error: --spool_dir is required"); return 1; } - { - conf.event_bindings.emplace_back(); - auto& b = conf.event_bindings.back(); - b.event_name = "sys.unix"; - b.interval_micros = 1000000; - b.sources.emplace_back(); - auto& s = b.sources.back(); - s.plugin_name = "unix_stats"; - } /* load plugins */ std::unique_ptr plugin_map(new PluginMap(&conf)); From c07352ad024a55e18bc71e9741112cdf0a1aded6 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 19:06:24 +0200 Subject: [PATCH 15/78] UnixStatsPlugin: report df -h stats --- .../plugins/unix_stats/unix_stats_plugin.cc | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index ad670e3..fc49997 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -56,14 +56,34 @@ ReturnCode UnixStatsPlugin::pluginGetNextEvent( struct statvfs buf; if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { - printf("statvfs failed /n"); continue; } + auto total = (double) (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); + auto available = (double) (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); + auto used = total - available; + + auto ifree = buf.f_favail; + auto iused = buf.f_files - ifree; + event_json->append(StringUtil::format( - R"({ "filesystem": "$0", "ifree": $1, "mount_point": "$2" })", + R"({ + "filesystem": "$0", + "total": $1, + "used": $2, + "available": $3, + "capacity": $4, + "iused": $5, + "ifree": $6, + "mount_point": "$7" + })", StringUtil::jsonEscape(mount_info[i].device), - buf.f_bsize * buf.f_bavail / (1024 * 1024), + total, + used, + available, + used / total, + iused, + ifree, StringUtil::jsonEscape(mount_info[i].mount_point))); } From d7e6de80579a2e1dc0c6c62665af300bd1952e8e Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 19:07:32 +0200 Subject: [PATCH 16/78] removed MountInfo.fs_type --- src/evcollect/plugins/unix_stats/unix_stats_plugin.cc | 2 +- src/evcollect/plugins/unix_stats/unix_stats_plugin.h | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index fc49997..a93366c 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -112,7 +112,7 @@ std::vector mount_info; for (size_t i = 0; i < mntsize; ++i) { UnixStatsPlugin::MountInfo mn_info = { .device = mntbuf[i].f_mntfromname, - .mount_point = mntbuf[i].f_mntonname //FIXME add type + .mount_point = mntbuf[i].f_mntonname }; mount_info.emplace_back(mn_info); diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.h b/src/evcollect/plugins/unix_stats/unix_stats_plugin.h index 23bbf1e..3f59111 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.h +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.h @@ -33,17 +33,9 @@ namespace plugin_unix_stats { class UnixStatsPlugin : public SourcePlugin { public: - enum fsType { - PROC, - SWAP, - EXT3, - EXT4 - }; - struct MountInfo { std::string device; std::string mount_point; - fsType fs_type; }; ReturnCode pluginGetNextEvent( From d6e4144ac1a0688dd0d22d05a22ba467b0ec60cc Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 20:36:47 +0200 Subject: [PATCH 17/78] mount info for linux --- src/evcollect/plugins/unix_stats/unix_stats_plugin.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index a93366c..60d010d 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -102,7 +102,12 @@ std::vector mount_info; #ifdef linux auto file = setmentent("/etc/fstab", "r"); - auto mntent = getmntent(file); + + while (auto mntent = getmntent(file)) { + printf("filesystemt: %s, mounted on: %s", mntent.mnt_fsname, mntent.mnt_dir); + } + + #elif __APPLE__ From 71139d6e2baa4a479902a54b721e068fb79e34a3 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 20:49:22 +0200 Subject: [PATCH 18/78] linux mnt_info --- .../plugins/unix_stats/unix_stats_plugin.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc index 60d010d..e3d5c70 100644 --- a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc +++ b/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc @@ -99,16 +99,18 @@ bool UnixStatsPlugin::pluginHasPendingEvent( std::vector UnixStatsPlugin::getMountInfo() { std::vector mount_info; - #ifdef linux + auto file = setmentent("/etc/fstab", "r"); - while (auto mntent = getmntent(file)) { printf("filesystemt: %s, mounted on: %s", mntent.mnt_fsname, mntent.mnt_dir); - } - - + UnixStatsPlugin::MountInfo mn_info = { + .device = mntent.mnt_fsname, + .mount_point = mntent.mnt_dir + }; + mount_info.emplace_back(mn_info); + } #elif __APPLE__ From 4b1046e185de5624c529fb4b701b114ed6348d1f Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 21:47:46 +0200 Subject: [PATCH 19/78] src/evcollect/pluginns/unix_stats -> plugins/unix_stats --- .../evcollect/plugins => plugins}/unix_stats/unix_stats_plugin.cc | 0 {src/evcollect/plugins => plugins}/unix_stats/unix_stats_plugin.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {src/evcollect/plugins => plugins}/unix_stats/unix_stats_plugin.cc (100%) rename {src/evcollect/plugins => plugins}/unix_stats/unix_stats_plugin.h (100%) diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc similarity index 100% rename from src/evcollect/plugins/unix_stats/unix_stats_plugin.cc rename to plugins/unix_stats/unix_stats_plugin.cc diff --git a/src/evcollect/plugins/unix_stats/unix_stats_plugin.h b/plugins/unix_stats/unix_stats_plugin.h similarity index 100% rename from src/evcollect/plugins/unix_stats/unix_stats_plugin.h rename to plugins/unix_stats/unix_stats_plugin.h From 63d37b5897e4a632d6b632958aa34fbcf509a11a Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Sun, 7 Aug 2016 22:09:53 +0200 Subject: [PATCH 20/78] unix stats makefile --- plugins/unix_stats/Makefile.am | 14 ++++++++++++++ plugins/unix_stats/unix_stats_plugin.cc | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 plugins/unix_stats/Makefile.am diff --git a/plugins/unix_stats/Makefile.am b/plugins/unix_stats/Makefile.am new file mode 100644 index 0000000..0103bba --- /dev/null +++ b/plugins/unix_stats/Makefile.am @@ -0,0 +1,14 @@ +MAINTAINERCLEANFILES = Makefile.in + +AM_CXXFLAGS = -std=c++0x -Wall -Wextra -Wdelete-non-virtual-dtor -g -fvisibility=hidden -I$(top_srcdir)/src +AM_CFLAGS = -std=c11 -Wall -pedantic -g +AM_LDFLAGS = -fvisibility=hidden + +lib_LTLIBRARIES = plugin_unix_stats.la + +plugin_unix_stats_la_LDFLAGS = -module -avoid-version -shared +plugin_unix_stats_la_SOURCES = \ + unix_stats_plugin.cc + + + diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index e3d5c70..6bbcedd 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -27,7 +27,7 @@ #include #include #include -#include "unix_stats_plugin.h" +#include #ifdef linux #include From 31d44d8bad70f6e73cf0f27b2d4a7890fad136f4 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 13:25:26 +0200 Subject: [PATCH 21/78] build unix stats plugin lib --- Makefile.am | 3 ++- configure.ac | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index de38e4b..715d2d8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,7 +7,8 @@ SUBDIRS = \ . \ src/evcollect \ plugins/eventql \ - plugins/hostname + plugins/hostname \ + plugins/unix_stats EXTRA_DIST = \ doc \ diff --git a/configure.ac b/configure.ac index 5691562..ad37b5a 100644 --- a/configure.ac +++ b/configure.ac @@ -64,5 +64,5 @@ AM_CONDITIONAL([HAVE_ZLIB], [test $HAVE_ZLIB = 1]) ACX_PTHREAD AM_CONDITIONAL([HAVE_PTHREAD], [test "x$acx_pthread_ok" = "xyes"]) -AC_CONFIG_FILES([Makefile src/evcollect/Makefile plugins/hostname/Makefile plugins/eventql/Makefile]) +AC_CONFIG_FILES([Makefile src/evcollect/Makefile plugins/hostname/Makefile plugins/eventql/Makefile plugins/unix_stats/Makefile]) AC_OUTPUT From 838bacfa1f4d30e88fdb149dfea4057b0f113fac Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 13:28:32 +0200 Subject: [PATCH 22/78] config: load unix stats plugin --- src/evcollect/config.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/evcollect/config.cc b/src/evcollect/config.cc index 31e7feb..23fadf5 100644 --- a/src/evcollect/config.cc +++ b/src/evcollect/config.cc @@ -30,6 +30,7 @@ ReturnCode loadConfig( const std::string& config_file_path, ProcessConfig* conf) { conf->load_plugins.push_back("./plugins/hostname/.libs/plugin_hostname.so"); + conf->load_plugins.push_back("./plugins/unix_stats/.libs/plugin_unix_stats.so"); conf->load_plugins.push_back("./plugins/eventql/.libs/plugin_eventql.so"); { @@ -43,6 +44,12 @@ ReturnCode loadConfig( b.sources.emplace_back(); auto& s = b.sources.back(); s.plugin_name = "hostname"; + + + // XXX: source plugin unix_stats + b.sources.emplace_back(); + auto& p = b.sources.back(); + p.plugin_name = "unix_stats"; } { From f9837634ce91e6268db07953ed31cd56d57d8a53 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 13:29:15 +0200 Subject: [PATCH 23/78] unix stats: getEvent --- plugins/unix_stats/Makefile.am | 12 ++- plugins/unix_stats/unix_stats_plugin.cc | 103 +++++++++++++----------- plugins/unix_stats/unix_stats_plugin.h | 57 ------------- 3 files changed, 66 insertions(+), 106 deletions(-) delete mode 100644 plugins/unix_stats/unix_stats_plugin.h diff --git a/plugins/unix_stats/Makefile.am b/plugins/unix_stats/Makefile.am index 0103bba..4b81fef 100644 --- a/plugins/unix_stats/Makefile.am +++ b/plugins/unix_stats/Makefile.am @@ -2,13 +2,19 @@ MAINTAINERCLEANFILES = Makefile.in AM_CXXFLAGS = -std=c++0x -Wall -Wextra -Wdelete-non-virtual-dtor -g -fvisibility=hidden -I$(top_srcdir)/src AM_CFLAGS = -std=c11 -Wall -pedantic -g -AM_LDFLAGS = -fvisibility=hidden +AM_LDFLAGS = -fvisibility=hidden -module -avoid-version -shared -export-dynamic -rpath $(libdir) -lib_LTLIBRARIES = plugin_unix_stats.la +noinst_LTLIBRARIES = plugin_unix_stats.la -plugin_unix_stats_la_LDFLAGS = -module -avoid-version -shared plugin_unix_stats_la_SOURCES = \ unix_stats_plugin.cc +PLUGINDIR=$(DESTDIR)$(libdir)/evcollect/plugins +install-data-hook: $(noinst_LTLIBRARIES) + @for soname in `echo | $(EGREP) "^dlname=" $^ | $(SED) -e "s|^dlname='\(.*\)'|\1|"`; do \ + mkdir -p ${PLUGINDIR}; \ + echo Installing $$soname to ${PLUGINDIR}; \ + cp $(abs_builddir)/.libs/$$soname ${PLUGINDIR}; \ + done diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 6bbcedd..15a72cc 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -22,12 +22,12 @@ * commercial activities involving this program without disclosing the source * code of your own applications */ +#include #include -#include #include #include +#include #include -#include #ifdef linux #include @@ -40,18 +40,57 @@ namespace evcollect { namespace plugin_unix_stats { -ReturnCode UnixStatsPlugin::pluginGetNextEvent( +struct MountInfo { + std::string device; + std::string mount_point; +}; + +static std::vector getMountInfo() { +std::vector mount_info; +#ifdef linux + + auto file = setmentent("/etc/fstab", "r"); + while (auto mntent = getmntent(file)) { + printf("filesystemt: %s, mounted on: %s", mntent.mnt_fsname, mntent.mnt_dir); + MountInfo mn_info = { + .device = mntent.mnt_fsname, + .mount_point = mntent.mnt_dir + }; + + mount_info.emplace_back(mn_info); + } + +#elif __APPLE__ + + struct statfs *mntbuf; + auto mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); + for (auto i = 0; i < mntsize; ++i) { + MountInfo mn_info = { + .device = mntbuf[i].f_mntfromname, + .mount_point = mntbuf[i].f_mntonname + }; + + mount_info.emplace_back(mn_info); + } + +#endif + + return mount_info; +} + +bool getEvent( + evcollect_ctx_t* ctx, void* userdata, - std::string* event_json) { + evcollect_event_t* ev) { std::string hostname; std::string hostname_fqdn; - event_json->append("(["); + std::string evdata = "(["; auto mount_info = getMountInfo(); for (size_t i = 0; i < mount_info.size(); ++i) { if (i > 0) { - event_json->append(","); + evdata.append(","); } struct statvfs buf; @@ -66,7 +105,7 @@ ReturnCode UnixStatsPlugin::pluginGetNextEvent( auto ifree = buf.f_favail; auto iused = buf.f_files - ifree; - event_json->append(StringUtil::format( + evdata.append(StringUtil::format( R"({ "filesystem": "$0", "total": $1, @@ -87,49 +126,21 @@ ReturnCode UnixStatsPlugin::pluginGetNextEvent( StringUtil::jsonEscape(mount_info[i].mount_point))); } - event_json->append("])"); - - return ReturnCode::success(); -} + evdata.append("])"); -bool UnixStatsPlugin::pluginHasPendingEvent( - void* userdata) { - return false; + evcollect_event_setdata(ev, evdata.data(), evdata.size()); + return true; } -std::vector UnixStatsPlugin::getMountInfo() { -std::vector mount_info; -#ifdef linux - - auto file = setmentent("/etc/fstab", "r"); - while (auto mntent = getmntent(file)) { - printf("filesystemt: %s, mounted on: %s", mntent.mnt_fsname, mntent.mnt_dir); - UnixStatsPlugin::MountInfo mn_info = { - .device = mntent.mnt_fsname, - .mount_point = mntent.mnt_dir - }; - - mount_info.emplace_back(mn_info); - } - -#elif __APPLE__ - - struct statfs *mntbuf; - auto mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); - for (size_t i = 0; i < mntsize; ++i) { - UnixStatsPlugin::MountInfo mn_info = { - .device = mntbuf[i].f_mntfromname, - .mount_point = mntbuf[i].f_mntonname - }; - - mount_info.emplace_back(mn_info); - } - -#endif - - return mount_info; -} } // namespace plugin_unix_stats } // namespace evcollect +bool __evcollect_plugin_init(evcollect_ctx_t* ctx) { + evcollect_source_plugin_register( + ctx, + "unix_stats", + &evcollect::plugin_unix_stats::getEvent); + + return true; +} diff --git a/plugins/unix_stats/unix_stats_plugin.h b/plugins/unix_stats/unix_stats_plugin.h deleted file mode 100644 index 3f59111..0000000 --- a/plugins/unix_stats/unix_stats_plugin.h +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (c) 2016 DeepCortex GmbH - * Authors: - * - Paul Asmuth - * - Laura Schlimmer - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU Affero General Public License ("the license") as - * published by the Free Software Foundation, either version 3 of the License, - * or any later version. - * - * In accordance with Section 7(e) of the license, the licensing of the Program - * under the license does not imply a trademark license. Therefore any rights, - * title and interest in our trademarks remain entirely with us. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the license for more details. - * - * You can be released from the requirements of the license by purchasing a - * commercial license. Buying such a license is mandatory as soon as you develop - * commercial activities involving this program without disclosing the source - * code of your own applications - */ -#pragma once -#include -#include -#include - -namespace evcollect { -namespace plugin_unix_stats { - -class UnixStatsPlugin : public SourcePlugin { -public: - - struct MountInfo { - std::string device; - std::string mount_point; - }; - - ReturnCode pluginGetNextEvent( - void* userdata, - std::string* event_json) override; - - bool pluginHasPendingEvent( - void* userdata) override; - - -protected: - - std::vector getMountInfo(); - -}; - -} // namespace plugin_unix_stats -} // namespace evcollect - From 03141bddd71718952e4397619467f42f4d790745 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 15:34:38 +0200 Subject: [PATCH 24/78] unix stats: fixed json --- plugins/unix_stats/unix_stats_plugin.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 15a72cc..375e267 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -85,7 +85,7 @@ bool getEvent( std::string hostname; std::string hostname_fqdn; - std::string evdata = "(["; + std::string evdata = "["; auto mount_info = getMountInfo(); for (size_t i = 0; i < mount_info.size(); ++i) { @@ -98,10 +98,10 @@ bool getEvent( continue; } - auto total = (double) (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); - auto available = (double) (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); + auto total = (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); + auto available = (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); auto used = total - available; - + auto capacity = total > 0 ? used / total : 1; auto ifree = buf.f_favail; auto iused = buf.f_files - ifree; @@ -120,13 +120,13 @@ bool getEvent( total, used, available, - used / total, + capacity, iused, ifree, StringUtil::jsonEscape(mount_info[i].mount_point))); } - evdata.append("])"); + evdata.append("]"); evcollect_event_setdata(ev, evdata.data(), evdata.size()); return true; From 5e920cfc71cbcf48638f744209486ff33ecc72e9 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 15:47:25 +0200 Subject: [PATCH 25/78] check for os --- plugins/unix_stats/unix_stats_plugin.cc | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 375e267..8efed3c 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -29,9 +29,11 @@ #include #include -#ifdef linux +#if __linux__ #include -#elif __APPLE__ +#endif + +#if __APPLE__ #include #include #include @@ -47,14 +49,13 @@ struct MountInfo { static std::vector getMountInfo() { std::vector mount_info; -#ifdef linux +#ifdef __linux__ - auto file = setmentent("/etc/fstab", "r"); + auto file = setmntent("/etc/mtab", "r"); while (auto mntent = getmntent(file)) { - printf("filesystemt: %s, mounted on: %s", mntent.mnt_fsname, mntent.mnt_dir); MountInfo mn_info = { - .device = mntent.mnt_fsname, - .mount_point = mntent.mnt_dir + .device = mntent->mnt_fsname, + .mount_point = mntent->mnt_dir }; mount_info.emplace_back(mn_info); @@ -73,6 +74,8 @@ std::vector mount_info; mount_info.emplace_back(mn_info); } +#else +#error "unsupported os" #endif return mount_info; From 45f25dbb75f429ecaaa370e57e529b80b545daaa Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 16:39:04 +0200 Subject: [PATCH 26/78] use MOUNTED --- plugins/unix_stats/unix_stats_plugin.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 8efed3c..d2b6ae9 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -51,7 +51,7 @@ static std::vector getMountInfo() { std::vector mount_info; #ifdef __linux__ - auto file = setmntent("/etc/mtab", "r"); + auto file = setmntent(MOUNTED, "r"); while (auto mntent = getmntent(file)) { MountInfo mn_info = { .device = mntent->mnt_fsname, From 16a4bb3515d28a96934c40467c82baa08259785b Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 17:08:23 +0200 Subject: [PATCH 27/78] read /proc/loadavg --- plugins/unix_stats/unix_stats_plugin.cc | 86 ++++++++++++++----------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index d2b6ae9..e154aec 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -88,48 +88,58 @@ bool getEvent( std::string hostname; std::string hostname_fqdn; - std::string evdata = "["; - - auto mount_info = getMountInfo(); - for (size_t i = 0; i < mount_info.size(); ++i) { - if (i > 0) { - evdata.append(","); - } - - struct statvfs buf; - if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { - continue; + std::string evdata = R"({"disk_stats": [)"; + + { + auto mount_info = getMountInfo(); + for (size_t i = 0; i < mount_info.size(); ++i) { + if (i > 0) { + evdata.append(","); + } + + struct statvfs buf; + if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { + continue; + } + + auto total = (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); + auto available = (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); + auto used = total - available; + auto capacity = total > 0 ? used / total : 1; + auto ifree = buf.f_favail; + auto iused = buf.f_files - ifree; + + evdata.append(StringUtil::format( + R"({ + "filesystem": "$0", + "total": $1, + "used": $2, + "available": $3, + "capacity": $4, + "iused": $5, + "ifree": $6, + "mount_point": "$7" + })", + StringUtil::jsonEscape(mount_info[i].device), + total, + used, + available, + capacity, + iused, + ifree, + StringUtil::jsonEscape(mount_info[i].mount_point))); } - auto total = (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); - auto available = (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); - auto used = total - available; - auto capacity = total > 0 ? used / total : 1; - auto ifree = buf.f_favail; - auto iused = buf.f_files - ifree; - - evdata.append(StringUtil::format( - R"({ - "filesystem": "$0", - "total": $1, - "used": $2, - "available": $3, - "capacity": $4, - "iused": $5, - "ifree": $6, - "mount_point": "$7" - })", - StringUtil::jsonEscape(mount_info[i].device), - total, - used, - available, - capacity, - iused, - ifree, - StringUtil::jsonEscape(mount_info[i].mount_point))); + evdata.append("]}"); } - evdata.append("]"); + +#if __linux__ + std::ifstream f("/proc/loadavg", std::ifstream::in); + while (f.good()) { + auto c = f.get(); + } +#endif evcollect_event_setdata(ev, evdata.data(), evdata.size()); return true; From 0e06c2bcc26f8d1a949c418d35687a1cb9ce62e2 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 8 Aug 2016 17:59:06 +0200 Subject: [PATCH 28/78] loadavg for osx --- plugins/unix_stats/unix_stats_plugin.cc | 31 ++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index e154aec..9ecaf1e 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -37,6 +37,8 @@ #include #include #include +#include +#include #endif namespace evcollect { @@ -133,14 +135,37 @@ bool getEvent( evdata.append("]}"); } + evdata.append(R"(,{"loadavg": )"); + #if __linux__ - std::ifstream f("/proc/loadavg", std::ifstream::in); - while (f.good()) { - auto c = f.get(); + //std::ifstream f("/proc/loadavg", std::ifstream::in); + //while (f.good()) { + // auto c = f.get(); + //} +#elif __APPLE__ + struct loadavg loadinfo; + size_t size = sizeof(loadinfo); + + if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { + //logerror strerror(errno)); + return false; + } + + for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { + if (i > 0) { + evdata.append(","); + } + + evdata.append(StringUtil::format( + "{$0: $1}", + i, + (double) loadinfo.ldavg[i] / loadinfo.fscale)); } #endif + evdata.append("}"); + evcollect_event_setdata(ev, evdata.data(), evdata.size()); return true; } From ed0156bbd2c25bbe6faa64277f684558ad3abfaa Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 10:03:07 +0200 Subject: [PATCH 29/78] print error if sysctlbyname fails --- plugins/unix_stats/unix_stats_plugin.cc | 53 ++++++++++++++----------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 9ecaf1e..29407d9 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -89,10 +89,11 @@ bool getEvent( evcollect_event_t* ev) { std::string hostname; std::string hostname_fqdn; + std::string evdata; - std::string evdata = R"({"disk_stats": [)"; - + //disk_stats { + evdata.append(R"({"disk_stats": [)"); auto mount_info = getMountInfo(); for (size_t i = 0; i < mount_info.size(); ++i) { if (i > 0) { @@ -135,36 +136,42 @@ bool getEvent( evdata.append("]}"); } - evdata.append(R"(,{"loadavg": )"); + //loadavg stats + { + + evdata.append(R"(,{"loadavg": )"); #if __linux__ - //std::ifstream f("/proc/loadavg", std::ifstream::in); - //while (f.good()) { - // auto c = f.get(); - //} + //std::ifstream f("/proc/loadavg", std::ifstream::in); + //while (f.good()) { + // auto c = f.get(); + //} #elif __APPLE__ - struct loadavg loadinfo; - size_t size = sizeof(loadinfo); + struct loadavg loadinfo; + size_t size = sizeof(loadinfo); + + if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { + evcollect_seterror( + ctx, + StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); + return false; + } - if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { - //logerror strerror(errno)); - return false; - } + for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { + if (i > 0) { + evdata.append(","); + } - for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { - if (i > 0) { - evdata.append(","); + evdata.append(StringUtil::format( + "{$0: $1}", + i, + (double) loadinfo.ldavg[i] / loadinfo.fscale)); } - - evdata.append(StringUtil::format( - "{$0: $1}", - i, - (double) loadinfo.ldavg[i] / loadinfo.fscale)); - } #endif - evdata.append("}"); + evdata.append("}"); + } evcollect_event_setdata(ev, evdata.data(), evdata.size()); return true; From ecc31ce962f2f849c78e2afaff12ed6c122707a6 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 10:48:59 +0200 Subject: [PATCH 30/78] wip --- plugins/unix_stats/unix_stats_plugin.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 29407d9..f22affe 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -31,6 +31,8 @@ #if __linux__ #include +#include +#include #endif #if __APPLE__ @@ -143,10 +145,14 @@ bool getEvent( #if __linux__ - //std::ifstream f("/proc/loadavg", std::ifstream::in); - //while (f.good()) { - // auto c = f.get(); - //} + char cur[4]; + std::ifstream f; + f.open("/proc/loadavg", std::ifstream::in); + f.get(cur, sizeof(cur), ' '); + printf("found %s", cur); + while (f.good()) { + + } #elif __APPLE__ struct loadavg loadinfo; size_t size = sizeof(loadinfo); From 506989f8905442698ec3518eb83ff4c2e5a0a3be Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 10:56:38 +0200 Subject: [PATCH 31/78] unix stats EVCOLLECT_PLUGIN_INIT --- plugins/unix_stats/unix_stats_plugin.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index f22affe..a9b1d28 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -187,7 +187,7 @@ bool getEvent( } // namespace plugin_unix_stats } // namespace evcollect -bool __evcollect_plugin_init(evcollect_ctx_t* ctx) { +EVCOLLECT_PLUGIN_INIT(unix_stats) { evcollect_source_plugin_register( ctx, "unix_stats", From 3085fadc13ff4c157702b59004f6fc64ef3b33f8 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 13:03:23 +0200 Subject: [PATCH 32/78] linux getloadavg --- plugins/unix_stats/unix_stats_plugin.cc | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index a9b1d28..220a3de 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -31,8 +31,6 @@ #if __linux__ #include -#include -#include #endif #if __APPLE__ @@ -145,13 +143,22 @@ bool getEvent( #if __linux__ - char cur[4]; - std::ifstream f; - f.open("/proc/loadavg", std::ifstream::in); - f.get(cur, sizeof(cur), ' '); - printf("found %s", cur); - while (f.good()) { + const size_t nelem = 3; + double loadavg[nelem]; + if (getloadavg(loadavg, 3) == -1) { + evcollect_seterror(ctx, "getloadavg failed"); + return false; + } + for (size_t i = 0; i < nelem; ++i) { + if (i > 0) { + evdata.append(","); + } + + evdata.append(StringUtil::format( + "{$0: $1}", + i, + loadavg[i])); } #elif __APPLE__ struct loadavg loadinfo; From 35fc51123df6d0f6094f6b9592bb5d1a7847365d Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 13:43:21 +0200 Subject: [PATCH 33/78] unix stats: sysinfo --- plugins/unix_stats/unix_stats_plugin.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 220a3de..216a318 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -31,6 +31,7 @@ #if __linux__ #include +#include #endif #if __APPLE__ @@ -143,14 +144,13 @@ bool getEvent( #if __linux__ - const size_t nelem = 3; - double loadavg[nelem]; - if (getloadavg(loadavg, 3) == -1) { - evcollect_seterror(ctx, "getloadavg failed"); + struct sysinfo info; + if (sysinfo(&info) == -1) { + evcollect_seterror(ctx, "sysinfo failed"); return false; } - for (size_t i = 0; i < nelem; ++i) { + for (size_t i = 0; i < sizeof(info.loads) / sizeof(info.loads[0]); ++i) { if (i > 0) { evdata.append(","); } @@ -158,8 +158,13 @@ bool getEvent( evdata.append(StringUtil::format( "{$0: $1}", i, - loadavg[i])); + info.loads[i])); } + + evdata.append(StringUtil::format(R"(,{"uptime": $0})", info.uptime)); + evdata.append(StringUtil::format(R"(,{"procs": $0})", info.procs)); + evdata.append(StringUtil::format(R"(,{"freeram": $0})", info.freeram)); + evdata.append(StringUtil::format(R"(,{"freeswap": $0})", info.freeswap)); #elif __APPLE__ struct loadavg loadinfo; size_t size = sizeof(loadinfo); From 5366edd8ed60935e8105f1f3c4de82cc9248696b Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 15:46:20 +0200 Subject: [PATCH 34/78] unix stats uptime plugin --- plugins/unix_stats/unix_stats_plugin.cc | 58 ++++++++++++++++++++++++- src/evcollect/config.cc | 26 +++++++---- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 216a318..7b71f62 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -24,10 +24,12 @@ */ #include #include +#include #include #include #include #include +#include #if __linux__ #include @@ -88,8 +90,6 @@ bool getEvent( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev) { - std::string hostname; - std::string hostname_fqdn; std::string evdata; //disk_stats @@ -195,6 +195,50 @@ bool getEvent( return true; } +bool getUptimeEvent( + evcollect_ctx_t* ctx, + void* userdata, + evcollect_event_t* ev) { + std::string evdata; +#if __linux__ + struct sysinfo info; + if (sysinfo(&info) == -1) { + evcollect_seterror(ctx, "sysinfo failed"); + return false; + } + + auto uptime = info.uptime; + +#elif __APPLE__ + struct timeval t; + size_t size = sizeof(t); + + if (sysctlbyname("kern.boottime", &t, &size, NULL, 0) == -1) { + evcollect_seterror( + ctx, + StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); + return false; + } + + auto uptime = t.tv_sec; + +#endif + + time_t now; + time(&now); + auto uptime_seconds = now - uptime; + + evdata.append(StringUtil::format( + R"({"days": $0, "hours": $1, "minutes": $2, "seconds": $3})", + uptime_seconds / kSecondsPerDay, + uptime_seconds / kSecondsPerHour, + uptime_seconds / kSecondsPerMinute, + uptime_seconds)); + + evcollect_event_setdata(ev, evdata.data(), evdata.size()); + return true; +} + } // namespace plugin_unix_stats } // namespace evcollect @@ -207,3 +251,13 @@ EVCOLLECT_PLUGIN_INIT(unix_stats) { return true; } + +EVCOLLECT_PLUGIN_INIT(unix_uptime) { + evcollect_source_plugin_register( + ctx, + "unix_uptime", + &evcollect::plugin_unix_stats::getUptimeEvent); + + return true; +} + diff --git a/src/evcollect/config.cc b/src/evcollect/config.cc index 73e4de0..bba85fc 100644 --- a/src/evcollect/config.cc +++ b/src/evcollect/config.cc @@ -63,16 +63,26 @@ ReturnCode loadConfig( b.event_name = "sys.alive"; b.interval_micros = 1000000; - // XXX: source plugin hostname - b.sources.emplace_back(); - auto& s = b.sources.back(); - s.plugin_name = "hostname"; + { + // XXX: source plugin hostname + b.sources.emplace_back(); + auto& s = b.sources.back(); + s.plugin_name = "hostname"; + } + { + // XXX: source plugin unix_stats + b.sources.emplace_back(); + auto& s = b.sources.back(); + s.plugin_name = "unix_stats"; + } - // XXX: source plugin unix_stats - b.sources.emplace_back(); - auto& p = b.sources.back(); - p.plugin_name = "unix_stats"; + { + // XXX: source plugin unix_stats + b.sources.emplace_back(); + auto& s = b.sources.back(); + s.plugin_name = "unix_uptime"; + } } { From dd33bfaaa56cf073db6367d48317756c5c680b33 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 15:49:45 +0200 Subject: [PATCH 35/78] init unix stats --- plugins/unix_stats/unix_stats_plugin.cc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 7b71f62..8a64569 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -236,6 +236,7 @@ bool getUptimeEvent( uptime_seconds)); evcollect_event_setdata(ev, evdata.data(), evdata.size()); + printf("event: %s", evdata.c_str()); return true; } @@ -249,10 +250,6 @@ EVCOLLECT_PLUGIN_INIT(unix_stats) { "unix_stats", &evcollect::plugin_unix_stats::getEvent); - return true; -} - -EVCOLLECT_PLUGIN_INIT(unix_uptime) { evcollect_source_plugin_register( ctx, "unix_uptime", @@ -260,4 +257,3 @@ EVCOLLECT_PLUGIN_INIT(unix_uptime) { return true; } - From 044f576a9361f28e3e9d0848481b3725af79faaf Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 16:00:16 +0200 Subject: [PATCH 36/78] uptime plugin: better time handling --- plugins/unix_stats/unix_stats_plugin.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 8a64569..f612be1 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -24,7 +24,6 @@ */ #include #include -#include #include #include #include @@ -224,9 +223,8 @@ bool getUptimeEvent( #endif - time_t now; - time(&now); - auto uptime_seconds = now - uptime; + UnixTime now; + auto uptime_seconds = (now.unixMicros() / kMicrosPerSecond) - uptime; evdata.append(StringUtil::format( R"({"days": $0, "hours": $1, "minutes": $2, "seconds": $3})", From 85a3f2bdd91afd8430ad2bac26c90e272ef38e34 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Tue, 9 Aug 2016 16:04:49 +0200 Subject: [PATCH 37/78] linux uptime stats --- plugins/unix_stats/unix_stats_plugin.cc | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index f612be1..d3dbd16 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -219,22 +219,18 @@ bool getUptimeEvent( return false; } - auto uptime = t.tv_sec; - + UnixTime now; + auto uptime = (now.unixMicros() / kMicrosPerSecond) - t.tv_sec; #endif - UnixTime now; - auto uptime_seconds = (now.unixMicros() / kMicrosPerSecond) - uptime; - evdata.append(StringUtil::format( R"({"days": $0, "hours": $1, "minutes": $2, "seconds": $3})", - uptime_seconds / kSecondsPerDay, - uptime_seconds / kSecondsPerHour, - uptime_seconds / kSecondsPerMinute, - uptime_seconds)); + uptime / kSecondsPerDay, + uptime / kSecondsPerHour, + uptime / kSecondsPerMinute, + uptime)); evcollect_event_setdata(ev, evdata.data(), evdata.size()); - printf("event: %s", evdata.c_str()); return true; } From d7e1b18bec1f0517390176e895743ac3de96b583 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 11:26:38 +0200 Subject: [PATCH 38/78] splitted unix stats in different events and created a combined event --- plugins/unix_stats/unix_stats_plugin.cc | 189 +++++++++++++----------- 1 file changed, 104 insertions(+), 85 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index d3dbd16..9d8c939 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -85,111 +85,111 @@ std::vector mount_info; return mount_info; } -bool getEvent( +bool getDiskUsageEvent( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev) { std::string evdata; - //disk_stats - { - evdata.append(R"({"disk_stats": [)"); - auto mount_info = getMountInfo(); - for (size_t i = 0; i < mount_info.size(); ++i) { - if (i > 0) { - evdata.append(","); - } - - struct statvfs buf; - if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { - continue; - } - - auto total = (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); - auto available = (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); - auto used = total - available; - auto capacity = total > 0 ? used / total : 1; - auto ifree = buf.f_favail; - auto iused = buf.f_files - ifree; - - evdata.append(StringUtil::format( - R"({ - "filesystem": "$0", - "total": $1, - "used": $2, - "available": $3, - "capacity": $4, - "iused": $5, - "ifree": $6, - "mount_point": "$7" - })", - StringUtil::jsonEscape(mount_info[i].device), - total, - used, - available, - capacity, - iused, - ifree, - StringUtil::jsonEscape(mount_info[i].mount_point))); + evdata.append(R"({"disk_stats": [)"); + auto mount_info = getMountInfo(); + for (size_t i = 0; i < mount_info.size(); ++i) { + if (i > 0) { + evdata.append(","); } - evdata.append("]}"); - } - - //loadavg stats - { + struct statvfs buf; + if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { + continue; + } - evdata.append(R"(,{"loadavg": )"); + auto total = (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); + auto available = (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); + auto used = total - available; + auto capacity = total > 0 ? used / total : 1; + auto ifree = buf.f_favail; + auto iused = buf.f_files - ifree; + + evdata.append(StringUtil::format( + R"({ + "filesystem": "$0", + "total": $1, + "used": $2, + "available": $3, + "capacity": $4, + "iused": $5, + "ifree": $6, + "mount_point": "$7" + })", + StringUtil::jsonEscape(mount_info[i].device), + total, + used, + available, + capacity, + iused, + ifree, + StringUtil::jsonEscape(mount_info[i].mount_point))); + } + evdata.append("]}"); + evcollect_event_setdata(ev, evdata.data(), evdata.size()); + return true; +} +bool getLoadAvgEvent( + evcollect_ctx_t* ctx, + void* userdata, + evcollect_event_t* ev) { + std::string evdata; #if __linux__ - struct sysinfo info; - if (sysinfo(&info) == -1) { - evcollect_seterror(ctx, "sysinfo failed"); - return false; + struct sysinfo info; + if (sysinfo(&info) == -1) { + evcollect_seterror(ctx, "sysinfo failed"); + return false; + } + + /* load average for the last 1, 5 and 15 minutes */ + for (size_t i = 0; i < sizeof(info.loads) / sizeof(info.loads[0]); ++i) { + if (i > 0) { + evdata.append(","); } - for (size_t i = 0; i < sizeof(info.loads) / sizeof(info.loads[0]); ++i) { - if (i > 0) { - evdata.append(","); - } + evdata.append(StringUtil::format( + "{$0: $1}", + i, + info.loads[i])); + } - evdata.append(StringUtil::format( - "{$0: $1}", - i, - info.loads[i])); - } + evdata.append(StringUtil::format(R"(,{"procs": $0})", info.procs)); + evdata.append(StringUtil::format(R"(,{"freeram": $0})", info.freeram)); + evdata.append(StringUtil::format(R"(,{"freeswap": $0})", info.freeswap)); - evdata.append(StringUtil::format(R"(,{"uptime": $0})", info.uptime)); - evdata.append(StringUtil::format(R"(,{"procs": $0})", info.procs)); - evdata.append(StringUtil::format(R"(,{"freeram": $0})", info.freeram)); - evdata.append(StringUtil::format(R"(,{"freeswap": $0})", info.freeswap)); #elif __APPLE__ - struct loadavg loadinfo; - size_t size = sizeof(loadinfo); + struct loadavg loadinfo; + size_t size = sizeof(loadinfo); + + if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { + evcollect_seterror( + ctx, + StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); + return false; + } - if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { - evcollect_seterror( - ctx, - StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); - return false; + /* load average for the last 1, 5 and 15 minutes */ + for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { + if (i > 0) { + evdata.append(","); } - for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { - if (i > 0) { - evdata.append(","); - } + evdata.append(StringUtil::format( + "{$0: $1}", + i, + (double) loadinfo.ldavg[i] / loadinfo.fscale)); + } - evdata.append(StringUtil::format( - "{$0: $1}", - i, - (double) loadinfo.ldavg[i] / loadinfo.fscale)); - } #endif - evdata.append("}"); - } - + evdata.append("}"); evcollect_event_setdata(ev, evdata.data(), evdata.size()); return true; } @@ -234,6 +234,16 @@ bool getUptimeEvent( return true; } +bool getEvent( + evcollect_ctx_t* ctx, + void* userdata, + evcollect_event_t* ev) { + if (!getUptimeEvent(ctx, userdata, ev) || + !getLoadAvgEvent(ctx, userdata, ev) || + !getDiskUsageEvent(ctx, userdata, ev)) { + return false; + } +} } // namespace plugin_unix_stats } // namespace evcollect @@ -241,13 +251,22 @@ bool getUptimeEvent( EVCOLLECT_PLUGIN_INIT(unix_stats) { evcollect_source_plugin_register( ctx, - "unix_stats", + "unix.stats", &evcollect::plugin_unix_stats::getEvent); evcollect_source_plugin_register( ctx, - "unix_uptime", + "unix.uptime", &evcollect::plugin_unix_stats::getUptimeEvent); + evcollect_source_plugin_register( + ctx, + "unix.load_avg", + &evcollect::plugin_unix_stats::getLoadAvgEvent); + + evcollect_source_plugin_register( + ctx, + "unix.disk_usage", + &evcollect::plugin_unix_stats::getDiskUsageEvent); return true; } From c86a41d3600716e0725eb70ae49ec2b0273d8b54 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 11:48:34 +0200 Subject: [PATCH 39/78] unix.stats doc --- plugins/unix_stats/unix_stats_plugin.md | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 plugins/unix_stats/unix_stats_plugin.md diff --git a/plugins/unix_stats/unix_stats_plugin.md b/plugins/unix_stats/unix_stats_plugin.md new file mode 100644 index 0000000..b3f17fc --- /dev/null +++ b/plugins/unix_stats/unix_stats_plugin.md @@ -0,0 +1,30 @@ +# evcollect unix.stats + +## Tables + +###uptime + + + + + + + + + + + + + + + + + + + + + + + + +
daysIntegerDays of uptime
hoursIntegerHours of uptime
minutesIntegerMinutes of uptime
secondsIntegerSeconds of uptime
From 4f68091a6a5e07bce1c57e721708970d5cc25492 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 11:48:47 +0200 Subject: [PATCH 40/78] rename unix_stats -> unix.stats --- src/evcollect/config.cc | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/evcollect/config.cc b/src/evcollect/config.cc index bba85fc..02c45ff 100644 --- a/src/evcollect/config.cc +++ b/src/evcollect/config.cc @@ -74,14 +74,7 @@ ReturnCode loadConfig( // XXX: source plugin unix_stats b.sources.emplace_back(); auto& s = b.sources.back(); - s.plugin_name = "unix_stats"; - } - - { - // XXX: source plugin unix_stats - b.sources.emplace_back(); - auto& s = b.sources.back(); - s.plugin_name = "unix_uptime"; + s.plugin_name = "unix.stats"; } } @@ -138,7 +131,7 @@ ReturnCode loadConfig( b.interval_micros = 1000000; b.sources.emplace_back(); auto& s = b.sources.back(); - s.plugin_name = "unix_stats"; + s.plugin_name = "unix.stats"; } return ReturnCode::success(); } From 58cec0c8b379dcb8eb673abfd0d3b7a79b6da49f Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 12:24:05 +0200 Subject: [PATCH 41/78] doc --- plugins/unix_stats/unix_stats_plugin.cc | 13 ++-- plugins/unix_stats/unix_stats_plugin.md | 82 ++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 9d8c939..6fe7851 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -103,8 +103,8 @@ bool getDiskUsageEvent( continue; } - auto total = (buf.f_blocks * buf.f_frsize) / (1024 * 1024 * 1024); - auto available = (buf.f_bavail * buf.f_frsize) / (1024 * 1024 * 1024); + auto total = (buf.f_blocks * buf.f_frsize); + auto available = (buf.f_bavail * buf.f_frsize); auto used = total - available; auto capacity = total > 0 ? used / total : 1; auto ifree = buf.f_favail; @@ -125,7 +125,7 @@ bool getDiskUsageEvent( total, used, available, - capacity, + capacity * 100, iused, ifree, StringUtil::jsonEscape(mount_info[i].mount_point))); @@ -141,6 +141,7 @@ bool getLoadAvgEvent( void* userdata, evcollect_event_t* ev) { std::string evdata; + #if __linux__ struct sysinfo info; if (sysinfo(&info) == -1) { @@ -149,6 +150,8 @@ bool getLoadAvgEvent( } /* load average for the last 1, 5 and 15 minutes */ + evdata.append(R"("load_avg" : [)"); + for (size_t i = 0; i < sizeof(info.loads) / sizeof(info.loads[0]); ++i) { if (i > 0) { evdata.append(","); @@ -160,7 +163,7 @@ bool getLoadAvgEvent( info.loads[i])); } - evdata.append(StringUtil::format(R"(,{"procs": $0})", info.procs)); + evdata.append(StringUtil::format(R"(],{"procs": $0})", info.procs)); evdata.append(StringUtil::format(R"(,{"freeram": $0})", info.freeram)); evdata.append(StringUtil::format(R"(,{"freeswap": $0})", info.freeswap)); @@ -176,6 +179,8 @@ bool getLoadAvgEvent( } /* load average for the last 1, 5 and 15 minutes */ + evdata.append(R"("load_avg" : [)"); + for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { if (i > 0) { evdata.append(","); diff --git a/plugins/unix_stats/unix_stats_plugin.md b/plugins/unix_stats/unix_stats_plugin.md index b3f17fc..e80d71f 100644 --- a/plugins/unix_stats/unix_stats_plugin.md +++ b/plugins/unix_stats/unix_stats_plugin.md @@ -2,8 +2,7 @@ ## Tables -###uptime - +###unix.uptime @@ -28,3 +27,82 @@
+ + +###unix.disk_usage + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
disk_stats.filesystemStringThe name of the file system.
disk_stats.mount_pointStringThe mount point.
disk_stats.totalIntegerThe total size of the file system in bytes.
disk_stats.usedIntegerThe total space used in the file system in bytes
disk_stats.availableIntegerThe total free space in bytes.
disk_stats.capacityIntegerThe percentage of space that is used between 0 and 100.
disk_stats.iusedIntegerThe number of used inodes.
disk_stats.ifreeIntegerThe number of free inodes.
+ + +###unix.load_avg + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
load_avgRepeated record
load_avg.minIntegerOne of 1, 5 or 15
load_avg.valueDoubleThe load average
freeramIntegerThe size of available memory.
freeswapIntegerThe size of available swap space.
From b671b64c6372a244c52de411fba62414257911fd Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 14:30:56 +0200 Subject: [PATCH 42/78] unix.processes: get process list for osx --- plugins/unix_stats/unix_stats_plugin.cc | 69 +++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 6fe7851..cdcc699 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -41,6 +41,7 @@ #include #include #include +#include #endif namespace evcollect { @@ -239,15 +240,70 @@ bool getUptimeEvent( return true; } -bool getEvent( +bool getProcessesEvent( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev) { - if (!getUptimeEvent(ctx, userdata, ev) || - !getLoadAvgEvent(ctx, userdata, ev) || - !getDiskUsageEvent(ctx, userdata, ev)) { + std::string evdata; + +#if __linux__ + + +#elif __APPLE__ + size_t len = 0; + int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL}; + if (sysctl(mib, 3, NULL, &len, NULL, 0) == -1) { + evcollect_seterror( + ctx, + StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); return false; } + + struct kinfo_proc *info; + info = static_cast(malloc(len)); + if (sysctl(mib, 3, info, &len, NULL, 0) == -1) { + evcollect_seterror( + ctx, + StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); + free(info); + return false; + } + + int count = len / sizeof(kinfo_proc); + for (int i = 0; i < count; ++i) { + pid_t pid = info[i].kp_proc.p_pid; + if (pid == 0) { + continue; + } + + uid_t uid = info[i].kp_eproc.e_ucred.cr_uid; + struct passwd *user = getpwuid(uid); + const char* username = user ? user->pw_name : ""; + + evdata.append(StringUtil::format( + R"({"pid": $0, "uid": $1, "username": "$2", "parent": $3, "group": $4})", + pid, + uid, + username, + info[i].kp_eproc.e_ppid, + info[i].kp_eproc.e_pgid)); + } + + free(info); +#endif + + evcollect_event_setdata(ev, evdata.data(), evdata.size()); + return true; +} + +bool getEvent( + evcollect_ctx_t* ctx, + void* userdata, + evcollect_event_t* ev) { + return (!getUptimeEvent(ctx, userdata, ev) || + !getLoadAvgEvent(ctx, userdata, ev) || + !getDiskUsageEvent(ctx, userdata, ev) || + !getProcessesEvent(ctx, userdata, ev)); } } // namespace plugin_unix_stats @@ -273,5 +329,10 @@ EVCOLLECT_PLUGIN_INIT(unix_stats) { ctx, "unix.disk_usage", &evcollect::plugin_unix_stats::getDiskUsageEvent); + + evcollect_source_plugin_register( + ctx, + "unix.processes", + &evcollect::plugin_unix_stats::getProcessesEvent); return true; } From cadc7c06ee88397cdc5a1d9d22d61416c6b60822 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 15:19:47 +0200 Subject: [PATCH 43/78] open proc/{pid} directories --- plugins/unix_stats/unix_stats_plugin.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index cdcc699..522a8ea 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -32,6 +32,7 @@ #if __linux__ #include +#include #include #endif @@ -247,7 +248,18 @@ bool getProcessesEvent( std::string evdata; #if __linux__ - + DIR *dir; + if (!(dir = opendir("/proc/")) { + evcollect_seterror( + ctx, + StringUtil::format("opendir('proc') failed: $0", strerror(errno)).c_str()); + return false; + } + + struct dirent *entry; + while (!(entry = readdir(dir)) { + printf("directory name: %s", dir.d_name); + } #elif __APPLE__ size_t len = 0; From e7a666a9994013fa806916280a59e3fb069f89c6 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 16:49:32 +0200 Subject: [PATCH 44/78] fix --- plugins/unix_stats/unix_stats_plugin.cc | 53 ++++++++++++++++++++++--- src/evcollect/service.cc | 9 ++--- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 522a8ea..26c3515 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -31,6 +31,7 @@ #include #if __linux__ +#include #include #include #include @@ -248,17 +249,57 @@ bool getProcessesEvent( std::string evdata; #if __linux__ - DIR *dir; - if (!(dir = opendir("/proc/")) { + auto dir = opendir("/proc/"); + if (!dir) { evcollect_seterror( ctx, - StringUtil::format("opendir('proc') failed: $0", strerror(errno)).c_str()); + "opendir failed"); + //StringUtil::format("opendir failed: $0", strerror(errno)).c_str()); return false; } - struct dirent *entry; - while (!(entry = readdir(dir)) { - printf("directory name: %s", dir.d_name); + for (;;) { + auto entry = readdir(dir); + if (!entry) { + break; + } + + if (entry->d_type == DT_DIR) { + // continue; + } + + if (StringUtil::isNumber(entry->d_name)) { + + } + + //auto file = fopen( + // StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), + // "r"); + //if (!file) { + // evcollect_seterror( + // ctx, + // "fopen failed"); + // return false; + //} + + //char content[2048]; + //if (!fgets(content, 2048, file)) { + // evcollect_seterror( + // ctx, + // "fgets failed"); + // return false; + //} + + //std::regex rgx("\\d"); + //std::smatch match; + //if (!regex_search(static_cast(content), match, rgx)) { + // evcollect_seterror( + // ctx, + // "regex_search failed"); + // return false; + //} + + //printf("match %s", match[0]); } #elif __APPLE__ diff --git a/src/evcollect/service.cc b/src/evcollect/service.cc index b910500..f529bef 100644 --- a/src/evcollect/service.cc +++ b/src/evcollect/service.cc @@ -92,6 +92,7 @@ class ServiceImpl : public Service { std::string spool_dir_; std::string plugin_dir_; PluginMap plugin_map_; + PluginContext plugin_ctx_; std::vector> event_bindings_; std::vector> targets_; std::multiset< @@ -111,6 +112,7 @@ ServiceImpl::ServiceImpl( return a->next_tick < b->next_tick; }), listen_fd_(-1) { + plugin_ctx_.plugin_map = &plugin_map_; LogfileSourcePlugin::registerPlugin(&plugin_map_); if (pipe(wakeup_pipe_) < 0) { @@ -198,9 +200,6 @@ ReturnCode ServiceImpl::addTarget(const TargetConfig* binding) { } ReturnCode ServiceImpl::loadPlugin(const std::string& plugin) { - PluginContext plugin_ctx; - plugin_ctx.plugin_map = &plugin_map_; - std::vector path_candidates; std::string plugin_name; if (StringUtil::isShellSafe(plugin)) { @@ -229,7 +228,7 @@ ReturnCode ServiceImpl::loadPlugin(const std::string& plugin) { continue; } - auto rc = evcollect::loadPlugin(&plugin_ctx, plugin_name, path); + auto rc = evcollect::loadPlugin(&plugin_ctx_, plugin_name, path); if (rc.isSuccess()) { return rc; } else { @@ -253,7 +252,7 @@ ReturnCode ServiceImpl::loadPlugin(bool (*init_fn)(evcollect_ctx_t* ctx)) { PluginContext plugin_ctx; plugin_ctx.plugin_map = &plugin_map_; - auto rc = evcollect::loadPlugin(&plugin_ctx, init_fn); + auto rc = evcollect::loadPlugin(&plugin_ctx_, init_fn); if (rc.isSuccess()) { return rc; } else { From 01e8e108cc5e0648d310a6fe0d23893ac4443c67 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 16:51:43 +0200 Subject: [PATCH 45/78] fix --- src/evcollect/service.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/evcollect/service.cc b/src/evcollect/service.cc index b910500..f529bef 100644 --- a/src/evcollect/service.cc +++ b/src/evcollect/service.cc @@ -92,6 +92,7 @@ class ServiceImpl : public Service { std::string spool_dir_; std::string plugin_dir_; PluginMap plugin_map_; + PluginContext plugin_ctx_; std::vector> event_bindings_; std::vector> targets_; std::multiset< @@ -111,6 +112,7 @@ ServiceImpl::ServiceImpl( return a->next_tick < b->next_tick; }), listen_fd_(-1) { + plugin_ctx_.plugin_map = &plugin_map_; LogfileSourcePlugin::registerPlugin(&plugin_map_); if (pipe(wakeup_pipe_) < 0) { @@ -198,9 +200,6 @@ ReturnCode ServiceImpl::addTarget(const TargetConfig* binding) { } ReturnCode ServiceImpl::loadPlugin(const std::string& plugin) { - PluginContext plugin_ctx; - plugin_ctx.plugin_map = &plugin_map_; - std::vector path_candidates; std::string plugin_name; if (StringUtil::isShellSafe(plugin)) { @@ -229,7 +228,7 @@ ReturnCode ServiceImpl::loadPlugin(const std::string& plugin) { continue; } - auto rc = evcollect::loadPlugin(&plugin_ctx, plugin_name, path); + auto rc = evcollect::loadPlugin(&plugin_ctx_, plugin_name, path); if (rc.isSuccess()) { return rc; } else { @@ -253,7 +252,7 @@ ReturnCode ServiceImpl::loadPlugin(bool (*init_fn)(evcollect_ctx_t* ctx)) { PluginContext plugin_ctx; plugin_ctx.plugin_map = &plugin_map_; - auto rc = evcollect::loadPlugin(&plugin_ctx, init_fn); + auto rc = evcollect::loadPlugin(&plugin_ctx_, init_fn); if (rc.isSuccess()) { return rc; } else { From f98703b0ed05ac60d3495bcf8a3fbf0b6ef6bbfd Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 16:51:53 +0200 Subject: [PATCH 46/78] wip --- plugins/unix_stats/unix_stats_plugin.cc | 53 ++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 522a8ea..26c3515 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -31,6 +31,7 @@ #include #if __linux__ +#include #include #include #include @@ -248,17 +249,57 @@ bool getProcessesEvent( std::string evdata; #if __linux__ - DIR *dir; - if (!(dir = opendir("/proc/")) { + auto dir = opendir("/proc/"); + if (!dir) { evcollect_seterror( ctx, - StringUtil::format("opendir('proc') failed: $0", strerror(errno)).c_str()); + "opendir failed"); + //StringUtil::format("opendir failed: $0", strerror(errno)).c_str()); return false; } - struct dirent *entry; - while (!(entry = readdir(dir)) { - printf("directory name: %s", dir.d_name); + for (;;) { + auto entry = readdir(dir); + if (!entry) { + break; + } + + if (entry->d_type == DT_DIR) { + // continue; + } + + if (StringUtil::isNumber(entry->d_name)) { + + } + + //auto file = fopen( + // StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), + // "r"); + //if (!file) { + // evcollect_seterror( + // ctx, + // "fopen failed"); + // return false; + //} + + //char content[2048]; + //if (!fgets(content, 2048, file)) { + // evcollect_seterror( + // ctx, + // "fgets failed"); + // return false; + //} + + //std::regex rgx("\\d"); + //std::smatch match; + //if (!regex_search(static_cast(content), match, rgx)) { + // evcollect_seterror( + // ctx, + // "regex_search failed"); + // return false; + //} + + //printf("match %s", match[0]); } #elif __APPLE__ From 0f0f5c6cb38b017e68ec4940c75b478796d8cd8b Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 21:12:29 +0200 Subject: [PATCH 47/78] read /proc/{pid}/stat files --- plugins/unix_stats/unix_stats_plugin.cc | 65 +++++++++++++------------ 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 26c3515..0950227 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -264,42 +264,45 @@ bool getProcessesEvent( break; } - if (entry->d_type == DT_DIR) { - // continue; + if (entry->d_type != DT_DIR || (!StringUtil::isNumber(entry->d_name))) { + continue; + } + + auto file = fopen( + StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), + "r"); + if (!file) { + evcollect_seterror( + ctx, + "fopen failed"); + return false; + } + + + char content[2048]; + if (!fgets(content, 2048, file)) { + fclose(file); + evcollect_seterror( + ctx, + "fgets failed"); + return false; } - if (StringUtil::isNumber(entry->d_name)) { + fclose(file); + std::regex rgx("\\d"); + std::smatch match; + if (!regex_search(static_cast(content), match, rgx)) { + evcollect_seterror( + ctx, + "regex_search failed"); + return false; } - //auto file = fopen( - // StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), - // "r"); - //if (!file) { - // evcollect_seterror( - // ctx, - // "fopen failed"); - // return false; - //} - - //char content[2048]; - //if (!fgets(content, 2048, file)) { - // evcollect_seterror( - // ctx, - // "fgets failed"); - // return false; - //} - - //std::regex rgx("\\d"); - //std::smatch match; - //if (!regex_search(static_cast(content), match, rgx)) { - // evcollect_seterror( - // ctx, - // "regex_search failed"); - // return false; - //} - - //printf("match %s", match[0]); + for (size_t i = 0; i < match.size(); ++i) { + std::string blah = match.str() + ""; + printf("match %s", blah.c_str()); + } } #elif __APPLE__ From fd1766e408c20ed5d2738324dcdbcdafa71016ac Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Wed, 10 Aug 2016 21:43:45 +0200 Subject: [PATCH 48/78] proc/{pid}/stat regex --- plugins/unix_stats/unix_stats_plugin.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 0950227..674d0ed 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -290,7 +290,7 @@ bool getProcessesEvent( fclose(file); - std::regex rgx("\\d"); + std::regex rgx("(\\d+)\\s\((\\w+)\)\\s(\\w)"); std::smatch match; if (!regex_search(static_cast(content), match, rgx)) { evcollect_seterror( From c6c46eec23a9315d1ee0d2749d2612eef2e86f05 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 13:16:02 +0200 Subject: [PATCH 49/78] parse /proc/{pid}/stat file --- plugins/unix_stats/unix_stats_plugin.cc | 79 +++++++++++++++---------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 674d0ed..3f3479a 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -31,7 +31,7 @@ #include #if __linux__ -#include +#include #include #include #include @@ -268,41 +268,60 @@ bool getProcessesEvent( continue; } - auto file = fopen( + std::ifstream file( StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), - "r"); - if (!file) { - evcollect_seterror( - ctx, - "fopen failed"); + std::ifstream::in); + if (!file.is_open()) { return false; } - - char content[2048]; - if (!fgets(content, 2048, file)) { - fclose(file); - evcollect_seterror( - ctx, - "fgets failed"); - return false; + size_t i = 0; + std::string buf; + while (!file.eof()) { + char c; + file.get(c); + if (isspace(c)) { + switch (i++) { + case 0: + evdata.append(StringUtil::format(R"({"pid": $0)", buf)); + break; + + case 1: + evdata.append(StringUtil::format(R"({"ex_name": $0)", buf)); + break; + + default: + break; + } + + buf.clear(); + } else { + buf.push_back(c); + } } - fclose(file); - - std::regex rgx("(\\d+)\\s\((\\w+)\)\\s(\\w)"); - std::smatch match; - if (!regex_search(static_cast(content), match, rgx)) { - evcollect_seterror( - ctx, - "regex_search failed"); - return false; - } - - for (size_t i = 0; i < match.size(); ++i) { - std::string blah = match.str() + ""; - printf("match %s", blah.c_str()); - } + //auto file = fopen( + // StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), + // "r"); + //if (!file) { + // evcollect_seterror( + // ctx, + // "fopen failed"); + // return false; + //} + + + //char content[2048]; + //if (!fgets(content, 2048, file)) { + // fclose(file); + // evcollect_seterror( + // ctx, + // "fgets failed"); + // return false; + //} + + //fclose(file); + } #elif __APPLE__ From a834938498de89578a781048258f8a22622747af Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 15:44:10 +0200 Subject: [PATCH 50/78] parse /proc/{pid}/stat files and write values to json --- plugins/unix_stats/unix_stats_plugin.cc | 61 ++++++++++++++----------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 3f3479a..467be12 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -282,12 +282,44 @@ bool getProcessesEvent( file.get(c); if (isspace(c)) { switch (i++) { - case 0: + case 0: /* process id */ evdata.append(StringUtil::format(R"({"pid": $0)", buf)); break; - case 1: - evdata.append(StringUtil::format(R"({"ex_name": $0)", buf)); + case 1: /* filename of the executable */ + evdata.append(StringUtil::format(R"({"name": $0)", buf)); + break; + + case 2: /* process state */ + evdata.append(StringUtil::format(R"({"state": $0)", buf)); + break; + + case 3: /* parent PID */ + evdata.append(StringUtil::format(R"({"ppid": $0)", buf)); + break; + + case 4: /* group ID */ + evdata.append(StringUtil::format(R"({"pgrp": $0)", buf)); + break; + + case 13: /* time in user mode */ + evdata.append(StringUtil::format(R"({"utime": $0)", buf)); + break; + + case 14: /* time in kernel mode */ + evdata.append(StringUtil::format(R"({"stime": $0)", buf)); + break; + + case 18: /* nice value */ + evdata.append(StringUtil::format(R"({"nice": $0)", buf)); + break; + + case 21: /* starttime */ + evdata.append(StringUtil::format(R"({"starttime": $0)", buf)); + break; + + case 22: /* virtual memory size */ + evdata.append(StringUtil::format(R"({"vsize": $0)", buf)); break; default: @@ -299,29 +331,6 @@ bool getProcessesEvent( buf.push_back(c); } } - - //auto file = fopen( - // StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), - // "r"); - //if (!file) { - // evcollect_seterror( - // ctx, - // "fopen failed"); - // return false; - //} - - - //char content[2048]; - //if (!fgets(content, 2048, file)) { - // fclose(file); - // evcollect_seterror( - // ctx, - // "fgets failed"); - // return false; - //} - - //fclose(file); - } #elif __APPLE__ From 0cb6105b721887bf9d5c043f11c9163a941cf663 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 16:13:14 +0200 Subject: [PATCH 51/78] fixed process event json string --- plugins/unix_stats/unix_stats_plugin.cc | 36 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 467be12..404bc8d 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -258,6 +258,9 @@ bool getProcessesEvent( return false; } + evdata.append("["); + + size_t i = 0; for (;;) { auto entry = readdir(dir); if (!entry) { @@ -275,51 +278,57 @@ bool getProcessesEvent( return false; } - size_t i = 0; + if (i++ > 0) { + evdata.append(","); + } + + evdata.append("{"); + + size_t j = 0; std::string buf; while (!file.eof()) { char c; file.get(c); if (isspace(c)) { - switch (i++) { + switch (j++) { case 0: /* process id */ - evdata.append(StringUtil::format(R"({"pid": $0)", buf)); + evdata.append(StringUtil::format(R"("pid": $0)", buf)); break; case 1: /* filename of the executable */ - evdata.append(StringUtil::format(R"({"name": $0)", buf)); + evdata.append(StringUtil::format(R"(,"name": "$0")", buf)); break; case 2: /* process state */ - evdata.append(StringUtil::format(R"({"state": $0)", buf)); + evdata.append(StringUtil::format(R"(,"state": "$0")", buf)); break; case 3: /* parent PID */ - evdata.append(StringUtil::format(R"({"ppid": $0)", buf)); + evdata.append(StringUtil::format(R"(,"ppid": $0)", buf)); break; case 4: /* group ID */ - evdata.append(StringUtil::format(R"({"pgrp": $0)", buf)); + evdata.append(StringUtil::format(R"(,"pgrp": $0)", buf)); break; case 13: /* time in user mode */ - evdata.append(StringUtil::format(R"({"utime": $0)", buf)); + evdata.append(StringUtil::format(R"(,"utime": $0)", buf)); break; case 14: /* time in kernel mode */ - evdata.append(StringUtil::format(R"({"stime": $0)", buf)); + evdata.append(StringUtil::format(R"(,"stime": $0)", buf)); break; case 18: /* nice value */ - evdata.append(StringUtil::format(R"({"nice": $0)", buf)); + evdata.append(StringUtil::format(R"(,"nice": $0)", buf)); break; case 21: /* starttime */ - evdata.append(StringUtil::format(R"({"starttime": $0)", buf)); + evdata.append(StringUtil::format(R"(,"starttime": $0)", buf)); break; case 22: /* virtual memory size */ - evdata.append(StringUtil::format(R"({"vsize": $0)", buf)); + evdata.append(StringUtil::format(R"(,"vsize": $0)", buf)); break; default: @@ -330,8 +339,11 @@ bool getProcessesEvent( } else { buf.push_back(c); } + } + evdata.append("}"); } + evdata.append("]"); #elif __APPLE__ size_t len = 0; From bec4e5b3f2d60c3a2c7ab98f090321ccf42f0297 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 16:31:06 +0200 Subject: [PATCH 52/78] docu --- plugins/unix_stats/unix_stats_plugin.md | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/plugins/unix_stats/unix_stats_plugin.md b/plugins/unix_stats/unix_stats_plugin.md index e80d71f..b1cb5b0 100644 --- a/plugins/unix_stats/unix_stats_plugin.md +++ b/plugins/unix_stats/unix_stats_plugin.md @@ -106,3 +106,53 @@ + +###unix.processes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
process.pidThe process ID. +
process.nameThe filename of the executable. +
process.stateThe process state. +
process.ppidThe parent PID of the process. +
process.pgrpThe group ID of the process. +
process.ppidThe PID of the parent of the process. +
process.utimeThe time, measured in clock ticks, spent the process spent in user mode. +
process.stimeThe time, measured in clock ticks, spent the process spent in kernel mode. +
process.niceThe process nice level (-20 - 19). +
process.starttimeThe time the process started after system boot. +
process.vsizeThe virtual memory size in bytes. +
From 6e505128c829652839093aad27db4bbbaed8e7af Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 16:31:16 +0200 Subject: [PATCH 53/78] format --- plugins/unix_stats/unix_stats_plugin.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 404bc8d..3875d9a 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -341,8 +341,10 @@ bool getProcessesEvent( } } + evdata.append("}"); } + evdata.append("]"); #elif __APPLE__ From 3ff5891b1413640b868c087fa9634ef0a68e1af9 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 17:14:16 +0200 Subject: [PATCH 54/78] ... --- plugins/unix_stats/unix_stats_plugin.cc | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 3875d9a..68407fa 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -259,7 +259,6 @@ bool getProcessesEvent( } evdata.append("["); - size_t i = 0; for (;;) { auto entry = readdir(dir); @@ -349,7 +348,7 @@ bool getProcessesEvent( #elif __APPLE__ size_t len = 0; - int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL}; + int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; if (sysctl(mib, 3, NULL, &len, NULL, 0) == -1) { evcollect_seterror( ctx, @@ -360,10 +359,10 @@ bool getProcessesEvent( struct kinfo_proc *info; info = static_cast(malloc(len)); if (sysctl(mib, 3, info, &len, NULL, 0) == -1) { + free(info); evcollect_seterror( ctx, StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); - free(info); return false; } @@ -374,17 +373,20 @@ bool getProcessesEvent( continue; } - uid_t uid = info[i].kp_eproc.e_ucred.cr_uid; - struct passwd *user = getpwuid(uid); - const char* username = user ? user->pw_name : ""; - evdata.append(StringUtil::format( - R"({"pid": $0, "uid": $1, "username": "$2", "parent": $3, "group": $4})", + R"({ + "pid": $0, + "parent": $1, + "group": $2, + "nice": $3, + "starttime": $4, + "state": "$5"})", pid, - uid, - username, info[i].kp_eproc.e_ppid, - info[i].kp_eproc.e_pgid)); + info[i].kp_eproc.e_pgid, + info[i].kp_proc.p_nice, + info[i].kp_proc.p_un.__p_starttime.tv_sec, + info[i].kp_proc.p_stat)); } free(info); From 9532c4a668b6a3e4bd9842f1b71173c3c8c59d53 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Thu, 11 Aug 2016 21:24:34 +0200 Subject: [PATCH 55/78] moved disk info code to DiskInfo --- plugins/unix_stats/Makefile.am | 2 + plugins/unix_stats/disk_stats.cc | 129 ++++++++++++++++++++ plugins/unix_stats/disk_stats.h | 54 +++++++++ plugins/unix_stats/unix_stats_plugin.cc | 152 ++++++------------------ 4 files changed, 220 insertions(+), 117 deletions(-) create mode 100644 plugins/unix_stats/disk_stats.cc create mode 100644 plugins/unix_stats/disk_stats.h diff --git a/plugins/unix_stats/Makefile.am b/plugins/unix_stats/Makefile.am index 4b81fef..29d8e3f 100644 --- a/plugins/unix_stats/Makefile.am +++ b/plugins/unix_stats/Makefile.am @@ -7,6 +7,8 @@ AM_LDFLAGS = -fvisibility=hidden -module -avoid-version -shared -export-dynamic noinst_LTLIBRARIES = plugin_unix_stats.la plugin_unix_stats_la_SOURCES = \ + disk_stats.h \ + disk_stats.cc \ unix_stats_plugin.cc PLUGINDIR=$(DESTDIR)$(libdir)/evcollect/plugins diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc new file mode 100644 index 0000000..953c7d5 --- /dev/null +++ b/plugins/unix_stats/disk_stats.cc @@ -0,0 +1,129 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ + +#include +#include +#include +#include +#include +#if __linux__ +#include +#endif + +#if __APPLE__ +#include +#endif + +namespace evcollect { +namespace plugin_unix_stats { + +std::vector getMountInfo() { + std::vector mount_info; + +#if __linux__ + + auto file = setmntent(MOUNTED, "r"); + while (auto mntent = getmntent(file)) { + MountInfo mn_info = { + .device = mntent->mnt_fsname, + .mount_point = mntent->mnt_dir + }; + + mount_info.emplace_back(mn_info); + } + //FIXME close file + +#elif __APPLE__ + struct statfs* mntbuf; + auto mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); + for (auto i = 0; i < mntsize; ++i) { + MountInfo mn_info = { + .device = mntbuf[i].f_mntfromname, + .mount_point = mntbuf[i].f_mntonname + }; + + mount_info.emplace_back(mn_info); + } + +#endif + + return mount_info; +} + + +bool getDiskInfo(std::vector disk_info) { + auto mount_info = getMountInfo(); + + for (size_t i = 0; i < mount_info.size(); ++i) { + struct statvfs buf; + if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { + continue; + } + + DiskInfo di; + di.total = buf.f_blocks * buf.f_frsize; + di.available = buf.f_bavail * buf.f_frsize; + di.used = di.total - di.available; + di.capacity = di.total > 0 ? (di.used / di.total) * 100 : 100; + di.ifree = buf.f_favail; + di.iused = buf.f_files - di.ifree; + + disk_info.emplace_back(di); + } + + return true; +} + +std::string toJSON(std::vector disk_info) { + std::string json; + json.append(R"("disk_info": [)"); + + for (size_t i = 0; i < disk_info.size(); ++i) { + if (i > 0) { + json.append(","); + } + + json.append(StringUtil::format(R"({ + "total": $0, + "available": $1, + "used": $2, + "capacity": $3, + "ifree": $4, + "iused": $5})", + disk_info[i].total, + disk_info[i].available, + disk_info[i].used, + disk_info[i].capacity, + disk_info[i].ifree, + disk_info[i].used + )); + } + + json.append("]"); + return json; +} + +} //namespace plugin_unix_stats +} //namespace evcollect diff --git a/plugins/unix_stats/disk_stats.h b/plugins/unix_stats/disk_stats.h new file mode 100644 index 0000000..a4ebfe8 --- /dev/null +++ b/plugins/unix_stats/disk_stats.h @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ + +#include +#include + +namespace evcollect { +namespace plugin_unix_stats { + +struct MountInfo { + std::string device; + std::string mount_point; +}; + +struct DiskInfo { + std::string filesystem; + std::string mount_point; + uint64_t total; + uint64_t used; + uint64_t available; + uint8_t capacity; + uint64_t iused; + uint64_t ifree; +}; + + +bool getDiskInfo(DiskInfo disk_info); + +void toJSON(DiskInfo disk_info); + +} //namespace plugin_unix_stats +} //namespace evcollect diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 68407fa..3d2ef51 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -29,6 +29,7 @@ #include #include #include +#include #if __linux__ #include @@ -49,95 +50,6 @@ namespace evcollect { namespace plugin_unix_stats { -struct MountInfo { - std::string device; - std::string mount_point; -}; - -static std::vector getMountInfo() { -std::vector mount_info; -#ifdef __linux__ - - auto file = setmntent(MOUNTED, "r"); - while (auto mntent = getmntent(file)) { - MountInfo mn_info = { - .device = mntent->mnt_fsname, - .mount_point = mntent->mnt_dir - }; - - mount_info.emplace_back(mn_info); - } - -#elif __APPLE__ - - struct statfs *mntbuf; - auto mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); - for (auto i = 0; i < mntsize; ++i) { - MountInfo mn_info = { - .device = mntbuf[i].f_mntfromname, - .mount_point = mntbuf[i].f_mntonname - }; - - mount_info.emplace_back(mn_info); - } - -#else -#error "unsupported os" -#endif - - return mount_info; -} - -bool getDiskUsageEvent( - evcollect_ctx_t* ctx, - void* userdata, - evcollect_event_t* ev) { - std::string evdata; - - evdata.append(R"({"disk_stats": [)"); - auto mount_info = getMountInfo(); - for (size_t i = 0; i < mount_info.size(); ++i) { - if (i > 0) { - evdata.append(","); - } - - struct statvfs buf; - if (statvfs(mount_info[i].mount_point.c_str(), &buf) == -1) { - continue; - } - - auto total = (buf.f_blocks * buf.f_frsize); - auto available = (buf.f_bavail * buf.f_frsize); - auto used = total - available; - auto capacity = total > 0 ? used / total : 1; - auto ifree = buf.f_favail; - auto iused = buf.f_files - ifree; - - evdata.append(StringUtil::format( - R"({ - "filesystem": "$0", - "total": $1, - "used": $2, - "available": $3, - "capacity": $4, - "iused": $5, - "ifree": $6, - "mount_point": "$7" - })", - StringUtil::jsonEscape(mount_info[i].device), - total, - used, - available, - capacity * 100, - iused, - ifree, - StringUtil::jsonEscape(mount_info[i].mount_point))); - } - - evdata.append("]}"); - evcollect_event_setdata(ev, evdata.data(), evdata.size()); - return true; -} bool getLoadAvgEvent( evcollect_ctx_t* ctx, @@ -400,39 +312,45 @@ bool getEvent( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev) { - return (!getUptimeEvent(ctx, userdata, ev) || - !getLoadAvgEvent(ctx, userdata, ev) || - !getDiskUsageEvent(ctx, userdata, ev) || - !getProcessesEvent(ctx, userdata, ev)); + + DiskInfo disk_info; + if (!getDiskInfo(disk_info)) { + return false; + } + return true; + // return (!getUptimeEvent(ctx, userdata, ev) || + // !getLoadAvgEvent(ctx, userdata, ev) || + // !getDiskUsageEvent(ctx, userdata, ev) || + // !getProcessesEvent(ctx, userdata, ev)); } } // namespace plugin_unix_stats } // namespace evcollect EVCOLLECT_PLUGIN_INIT(unix_stats) { - evcollect_source_plugin_register( - ctx, - "unix.stats", - &evcollect::plugin_unix_stats::getEvent); - - evcollect_source_plugin_register( - ctx, - "unix.uptime", - &evcollect::plugin_unix_stats::getUptimeEvent); - - evcollect_source_plugin_register( - ctx, - "unix.load_avg", - &evcollect::plugin_unix_stats::getLoadAvgEvent); - - evcollect_source_plugin_register( - ctx, - "unix.disk_usage", - &evcollect::plugin_unix_stats::getDiskUsageEvent); - - evcollect_source_plugin_register( - ctx, - "unix.processes", - &evcollect::plugin_unix_stats::getProcessesEvent); + //evcollect_source_plugin_register( + // ctx, + // "unix.stats", + // &evcollect::plugin_unix_stats::getEvent); + + //evcollect_source_plugin_register( + // ctx, + // "unix.uptime", + // &evcollect::plugin_unix_stats::getUptimeEvent); + + //evcollect_source_plugin_register( + // ctx, + // "unix.load_avg", + // &evcollect::plugin_unix_stats::getLoadAvgEvent); + + //evcollect_source_plugin_register( + // ctx, + // "unix.disk_usage", + // &evcollect::plugin_unix_stats::getDiskUsageEvent); + + //evcollect_source_plugin_register( + // ctx, + // "unix.processes", + // &evcollect::plugin_unix_stats::getProcessesEvent); return true; } From 1f1ec6c2a02bae22630403c1686f2bd416b871b0 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 12:51:41 +0200 Subject: [PATCH 56/78] DiskInfo json --- plugins/unix_stats/Makefile.am | 2 + plugins/unix_stats/disk_stats.cc | 2 +- plugins/unix_stats/disk_stats.h | 2 +- plugins/unix_stats/kernel_stats.cc | 119 ++++++++++++++++++++++++ plugins/unix_stats/kernel_stats.h | 55 +++++++++++ plugins/unix_stats/unix_stats_plugin.cc | 111 ++-------------------- 6 files changed, 185 insertions(+), 106 deletions(-) create mode 100644 plugins/unix_stats/kernel_stats.cc create mode 100644 plugins/unix_stats/kernel_stats.h diff --git a/plugins/unix_stats/Makefile.am b/plugins/unix_stats/Makefile.am index 29d8e3f..07790f1 100644 --- a/plugins/unix_stats/Makefile.am +++ b/plugins/unix_stats/Makefile.am @@ -9,6 +9,8 @@ noinst_LTLIBRARIES = plugin_unix_stats.la plugin_unix_stats_la_SOURCES = \ disk_stats.h \ disk_stats.cc \ + kernel_stats.h \ + kernel_stats.cc \ unix_stats_plugin.cc PLUGINDIR=$(DESTDIR)$(libdir)/evcollect/plugins diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc index 953c7d5..5433f19 100644 --- a/plugins/unix_stats/disk_stats.cc +++ b/plugins/unix_stats/disk_stats.cc @@ -98,7 +98,7 @@ bool getDiskInfo(std::vector disk_info) { std::string toJSON(std::vector disk_info) { std::string json; - json.append(R"("disk_info": [)"); + json.append(R"([)"); for (size_t i = 0; i < disk_info.size(); ++i) { if (i > 0) { diff --git a/plugins/unix_stats/disk_stats.h b/plugins/unix_stats/disk_stats.h index a4ebfe8..f851d2d 100644 --- a/plugins/unix_stats/disk_stats.h +++ b/plugins/unix_stats/disk_stats.h @@ -48,7 +48,7 @@ struct DiskInfo { bool getDiskInfo(DiskInfo disk_info); -void toJSON(DiskInfo disk_info); +std::string toJSON(DiskInfo disk_info); } //namespace plugin_unix_stats } //namespace evcollect diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc new file mode 100644 index 0000000..386d2f6 --- /dev/null +++ b/plugins/unix_stats/kernel_stats.cc @@ -0,0 +1,119 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ + +#include +#include + +#if __linux__ +#include +#endif + +#if __APPLE__ +#include +#endif + + +namespace evcollect { +namespace plugin_unix_stats { + + +bool getKernelInfo(KernelInfo kernel_info) { +#if __linux__ + struct sysinfo info; + if (sysinfo(&info) == -1) { + //evcollect_seterror(ctx, "sysinfo failed"); + return false; + } + + /* load average for the last 1, 5 and 15 minutes */ + switch (sizeof(info.loads) / sizeof(info.loads[0])) { + case 3: + kernel_info.load_avg.min15 = info_loads[2]; + + case 2: + kernel_info.load_avg.min15 = info_loads[1]; + + case 1: + kernel_info.load_avg.min5 = info_loads[0]; + } + + kernel_info.uptime = uptime; + +#elif __APPLE__ + + /* load averages */ + { + struct loadavg loadinfo; + size_t size = sizeof(loadinfo); + + if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { + //evcollect_seterror( + // ctx, + // StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); + return false; + } + + /* load average for the last 1, 5 and 15 minutes */ + switch (sizeof(loadinfo.ldavg) / sizeof(fixpt_t)) { + case 3: + kernel_info.load_avg.min15 = (double) loadinfo.ldavg[2] / loadinfo.fscale; + + case 2: + kernel_info.load_avg.min15 = (double) loadinfo.ldavg[1] / loadinfo.fscale; + + case 1: + kernel_info.load_avg.min5 = (double) loadinfo.ldavg[0] / loadinfo.fscale; + } + } + + /* uptime */ + { + struct timeval t; + size_t size = sizeof(t); + + if (sysctlbyname("kern.boottime", &t, &size, NULL, 0) == -1) { + //evcollect_seterror( + // ctx, + // StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); + return false; + } + + UnixTime now; + kernel_info.uptime = (now.unixMicros() / kMicrosPerSecond) - t.tv_sec; + } + +#endif + + return true; +} + +std::string toJSON(KernelInfo kernel_info) { + +} + +} //namespace plugin_unix_stats +} //namespace evcollect + + diff --git a/plugins/unix_stats/kernel_stats.h b/plugins/unix_stats/kernel_stats.h new file mode 100644 index 0000000..b40f2db --- /dev/null +++ b/plugins/unix_stats/kernel_stats.h @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ + +#include +#include + +namespace evcollect { +namespace plugin_unix_stats { + +struct LoadAvg { + double min1; + double min5; + double min15; +}; + +struct KernelInfo { + uint64_t uptime; + plugin_unix_stats::LoadAvg load_avg; + + std::string version; + std::string arguments; + std::string path; + std::string device; +}; + + +bool getKernelInfo(KernelInfo kernel_info); + +std::string toJSON(KernelInfo kernel_info); + +} //namespace plugin_unix_stats +} //namespace evcollect + diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 3d2ef51..8b5ca56 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -30,6 +30,7 @@ #include #include #include +#include #if __linux__ #include @@ -50,110 +51,6 @@ namespace evcollect { namespace plugin_unix_stats { - -bool getLoadAvgEvent( - evcollect_ctx_t* ctx, - void* userdata, - evcollect_event_t* ev) { - std::string evdata; - -#if __linux__ - struct sysinfo info; - if (sysinfo(&info) == -1) { - evcollect_seterror(ctx, "sysinfo failed"); - return false; - } - - /* load average for the last 1, 5 and 15 minutes */ - evdata.append(R"("load_avg" : [)"); - - for (size_t i = 0; i < sizeof(info.loads) / sizeof(info.loads[0]); ++i) { - if (i > 0) { - evdata.append(","); - } - - evdata.append(StringUtil::format( - "{$0: $1}", - i, - info.loads[i])); - } - - evdata.append(StringUtil::format(R"(],{"procs": $0})", info.procs)); - evdata.append(StringUtil::format(R"(,{"freeram": $0})", info.freeram)); - evdata.append(StringUtil::format(R"(,{"freeswap": $0})", info.freeswap)); - -#elif __APPLE__ - struct loadavg loadinfo; - size_t size = sizeof(loadinfo); - - if (sysctlbyname("vm.loadavg", &loadinfo, &size, NULL, 0) == -1) { - evcollect_seterror( - ctx, - StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); - return false; - } - - /* load average for the last 1, 5 and 15 minutes */ - evdata.append(R"("load_avg" : [)"); - - for (size_t i = 0; i < sizeof(loadinfo.ldavg) / sizeof(fixpt_t); ++i) { - if (i > 0) { - evdata.append(","); - } - - evdata.append(StringUtil::format( - "{$0: $1}", - i, - (double) loadinfo.ldavg[i] / loadinfo.fscale)); - } - -#endif - - evdata.append("}"); - evcollect_event_setdata(ev, evdata.data(), evdata.size()); - return true; -} - -bool getUptimeEvent( - evcollect_ctx_t* ctx, - void* userdata, - evcollect_event_t* ev) { - std::string evdata; -#if __linux__ - struct sysinfo info; - if (sysinfo(&info) == -1) { - evcollect_seterror(ctx, "sysinfo failed"); - return false; - } - - auto uptime = info.uptime; - -#elif __APPLE__ - struct timeval t; - size_t size = sizeof(t); - - if (sysctlbyname("kern.boottime", &t, &size, NULL, 0) == -1) { - evcollect_seterror( - ctx, - StringUtil::format("sysctlbyname failed: $0", strerror(errno)).c_str()); - return false; - } - - UnixTime now; - auto uptime = (now.unixMicros() / kMicrosPerSecond) - t.tv_sec; -#endif - - evdata.append(StringUtil::format( - R"({"days": $0, "hours": $1, "minutes": $2, "seconds": $3})", - uptime / kSecondsPerDay, - uptime / kSecondsPerHour, - uptime / kSecondsPerMinute, - uptime)); - - evcollect_event_setdata(ev, evdata.data(), evdata.size()); - return true; -} - bool getProcessesEvent( evcollect_ctx_t* ctx, void* userdata, @@ -317,6 +214,12 @@ bool getEvent( if (!getDiskInfo(disk_info)) { return false; } + + KernelInfo kernel_info; + if (!getKernelInfo(kernel_info)) { + return false; + } + return true; // return (!getUptimeEvent(ctx, userdata, ev) || // !getLoadAvgEvent(ctx, userdata, ev) || From d38881416715868e4bd627b363f738c5d8aa9a22 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 13:44:16 +0200 Subject: [PATCH 57/78] make evcollect plugin api c compatible --- plugins/eventql/eventql_plugin.cc | 14 +++++---- plugins/hostname/hostname_plugin.cc | 9 ++++-- src/evcollect/evcollect.h | 49 ++++++++++++++++------------- src/evcollect/plugin.cc | 4 +-- src/evcollect/util/time.cc | 1 - 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/plugins/eventql/eventql_plugin.cc b/plugins/eventql/eventql_plugin.cc index 742f045..5f71cea 100644 --- a/plugins/eventql/eventql_plugin.cc +++ b/plugins/eventql/eventql_plugin.cc @@ -354,7 +354,7 @@ ReturnCode EventQLTarget::uploadEvent(const EnqueuedEvent& ev) { } } -bool pluginAttach( +int pluginAttach( evcollect_ctx_t* ctx, const evcollect_plugin_cfg_t* cfg, void** userdata) { @@ -457,14 +457,14 @@ bool pluginAttach( return true; } -bool pluginDetach(evcollect_ctx_t* ctx, void* userdata) { +int pluginDetach(evcollect_ctx_t* ctx, void* userdata) { auto target = static_cast(userdata); target->stopUploadThread(); delete target; return true; } -bool pluginEmitEvent( +int pluginEmitEvent( evcollect_ctx_t* ctx, void* userdata, const evcollect_event_t* event) { @@ -483,10 +483,10 @@ bool pluginEmitEvent( std::string(ev_data, ev_data_len)); if (rc.isSuccess()) { - return true; + return 1; } else { evcollect_seterror(ctx, rc.getMessage().c_str()); - return false; + return 0; } } @@ -499,7 +499,9 @@ EVCOLLECT_PLUGIN_INIT(eventql) { "eventql", &evcollect::plugin_eventql::pluginEmitEvent, &evcollect::plugin_eventql::pluginAttach, - &evcollect::plugin_eventql::pluginDetach); + &evcollect::plugin_eventql::pluginDetach, + NULL, + NULL); return true; } diff --git a/plugins/hostname/hostname_plugin.cc b/plugins/hostname/hostname_plugin.cc index 51d3356..1d27ed3 100644 --- a/plugins/hostname/hostname_plugin.cc +++ b/plugins/hostname/hostname_plugin.cc @@ -30,7 +30,7 @@ namespace evcollect { namespace plugin_hostname { -bool getEvent( +int getEvent( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev) { @@ -66,7 +66,12 @@ EVCOLLECT_PLUGIN_INIT(hostname) { evcollect_source_plugin_register( ctx, "hostname", - &evcollect::plugin_hostname::getEvent); + &evcollect::plugin_hostname::getEvent, + NULL, + NULL, + NULL, + NULL, + NULL); return true; } diff --git a/src/evcollect/evcollect.h b/src/evcollect/evcollect.h index 0c4f29f..b28c910 100644 --- a/src/evcollect/evcollect.h +++ b/src/evcollect/evcollect.h @@ -22,15 +22,16 @@ * code of your own applications */ #pragma once -#include -#include +#include /** * This file contains the common public C and C++ API as well as the C plugin * api */ +#ifdef __cplusplus extern "C" { +#endif typedef void evcollect_ctx_t; typedef void evcollect_plugin_cfg_t; @@ -52,15 +53,15 @@ enum evcollect_loglevel { }; void evcollect_log( - evcollect_loglevel level, + enum evcollect_loglevel level, const char* msg); -bool evcollect_plugin_getcfg( +int evcollect_plugin_getcfg( const evcollect_plugin_cfg_t* cfg, const char* key, const char** value); -bool evcollect_plugin_getcfgv( +int evcollect_plugin_getcfgv( const evcollect_plugin_cfg_t* cfg, const char* key, size_t i, @@ -87,30 +88,30 @@ void evcollect_event_setdata( const char* data, size_t size); -typedef bool (*evcollect_plugin_getnextevent_fn)( +typedef int (*evcollect_plugin_getnextevent_fn)( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev); -typedef bool (*evcollect_plugin_hasnextevent_fn)( +typedef int (*evcollect_plugin_hasnextevent_fn)( evcollect_ctx_t* ctx, void* userdata); -typedef bool (*evcollect_plugin_emitevent_fn)( +typedef int (*evcollect_plugin_emitevent_fn)( evcollect_ctx_t* ctx, void* userdata, const evcollect_event_t* ev); -typedef bool (*evcollect_plugin_attach_fn)( +typedef int (*evcollect_plugin_attach_fn)( evcollect_ctx_t* ctx, const evcollect_plugin_cfg_t* cfg, void** userdata); -typedef bool (*evcollect_plugin_detach_fn)( +typedef int (*evcollect_plugin_detach_fn)( evcollect_ctx_t* ctx, void* userdata); -typedef bool (*evcollect_plugin_init_fn)( +typedef int (*evcollect_plugin_init_fn)( evcollect_ctx_t* ctx); typedef void (*evcollect_plugin_free_fn)( @@ -120,30 +121,34 @@ void evcollect_source_plugin_register( evcollect_ctx_t* ctx, const char* plugin_name, evcollect_plugin_getnextevent_fn getnextevent_fn, - evcollect_plugin_hasnextevent_fn hasnextevent_fn = nullptr, - evcollect_plugin_attach_fn attach_fn = nullptr, - evcollect_plugin_detach_fn detach_fn = nullptr, - evcollect_plugin_init_fn init_fn = nullptr, - evcollect_plugin_free_fn free_fn = nullptr); + evcollect_plugin_hasnextevent_fn hasnextevent_fn, + evcollect_plugin_attach_fn attach_fn, + evcollect_plugin_detach_fn detach_fn, + evcollect_plugin_init_fn init_fn, + evcollect_plugin_free_fn free_fn); void evcollect_output_plugin_register( evcollect_ctx_t* ctx, const char* plugin_name, evcollect_plugin_emitevent_fn emitevent_fn, - evcollect_plugin_attach_fn attach_fn = nullptr, - evcollect_plugin_detach_fn detach_fn = nullptr, - evcollect_plugin_init_fn init_fn = nullptr, - evcollect_plugin_free_fn free_fn = nullptr); + evcollect_plugin_attach_fn attach_fn, + evcollect_plugin_detach_fn detach_fn, + evcollect_plugin_init_fn init_fn, + evcollect_plugin_free_fn free_fn); -bool __evcollect_plugin_init( +int __evcollect_plugin_init( evcollect_ctx_t* ctx); #define EVCOLLECT_PLUGIN_INIT(N) \ - extern "C" __attribute__((visibility("default"))) bool plugin_ ## N ## _init(evcollect_ctx_t* ctx) + extern "C" __attribute__((visibility("default"))) int plugin_ ## N ## _init(evcollect_ctx_t* ctx) +#ifdef __cplusplus } // extern "C" +#endif #ifdef __cplusplus +#include +#include namespace evcollect { diff --git a/src/evcollect/plugin.cc b/src/evcollect/plugin.cc index 68aac62..a9e6baf 100644 --- a/src/evcollect/plugin.cc +++ b/src/evcollect/plugin.cc @@ -379,7 +379,7 @@ void evcollect_seterror(evcollect_ctx_t* ctx, const char* error) { ctx_->error = std::string(error); } -bool evcollect_plugin_getcfg( +int evcollect_plugin_getcfg( const evcollect_plugin_cfg_t* cfg, const char* key, const char** value) { @@ -387,7 +387,7 @@ bool evcollect_plugin_getcfg( return cfg_->get(key, value); } -bool evcollect_plugin_getcfgv( +int evcollect_plugin_getcfgv( const evcollect_plugin_cfg_t* cfg, const char* key, size_t i, diff --git a/src/evcollect/util/time.cc b/src/evcollect/util/time.cc index 053f48e..c2e54fd 100644 --- a/src/evcollect/util/time.cc +++ b/src/evcollect/util/time.cc @@ -33,7 +33,6 @@ #endif #include "time.h" #include "stringutil.h" -#include "logging.h" UnixTime WallClock::now() { return UnixTime(WallClock::getUnixMicros()); From 0a75eb36a51c383239c0adfc4137348f9c41353d Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 13:44:28 +0200 Subject: [PATCH 58/78] unix stats plugin build --- plugins/unix_stats/Makefile.am | 8 +++++++- plugins/unix_stats/disk_stats.cc | 3 ++- plugins/unix_stats/kernel_stats.cc | 18 ++++++++++++++---- plugins/unix_stats/unix_stats_plugin.cc | 17 +++++++++-------- plugins/unix_stats/util/stringutil.cc | 1 + plugins/unix_stats/util/stringutil.h | 1 + plugins/unix_stats/util/stringutil_impl.h | 1 + plugins/unix_stats/util/time.cc | 1 + plugins/unix_stats/util/time.h | 1 + plugins/unix_stats/util/time_impl.h | 1 + 10 files changed, 38 insertions(+), 14 deletions(-) create mode 120000 plugins/unix_stats/util/stringutil.cc create mode 120000 plugins/unix_stats/util/stringutil.h create mode 120000 plugins/unix_stats/util/stringutil_impl.h create mode 120000 plugins/unix_stats/util/time.cc create mode 120000 plugins/unix_stats/util/time.h create mode 120000 plugins/unix_stats/util/time_impl.h diff --git a/plugins/unix_stats/Makefile.am b/plugins/unix_stats/Makefile.am index 07790f1..816fd2d 100644 --- a/plugins/unix_stats/Makefile.am +++ b/plugins/unix_stats/Makefile.am @@ -2,11 +2,17 @@ MAINTAINERCLEANFILES = Makefile.in AM_CXXFLAGS = -std=c++0x -Wall -Wextra -Wdelete-non-virtual-dtor -g -fvisibility=hidden -I$(top_srcdir)/src AM_CFLAGS = -std=c11 -Wall -pedantic -g -AM_LDFLAGS = -fvisibility=hidden -module -avoid-version -shared -export-dynamic -rpath $(libdir) +AM_LDFLAGS = -fvisibility=hidden -module -avoid-version -shared -export-dynamic -static-libstdc++ -static-libgcc -rpath $(libdir) noinst_LTLIBRARIES = plugin_unix_stats.la plugin_unix_stats_la_SOURCES = \ + util/stringutil.cc \ + util/stringutil.h \ + util/stringutil_impl.h \ + util/time.cc \ + util/time.h \ + util/time_impl.h \ disk_stats.h \ disk_stats.cc \ kernel_stats.h \ diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc index 5433f19..64fc85a 100644 --- a/plugins/unix_stats/disk_stats.cc +++ b/plugins/unix_stats/disk_stats.cc @@ -26,8 +26,9 @@ #include #include #include -#include #include +#include "disk_stats.h" + #if __linux__ #include #endif diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index 386d2f6..10dcdc5 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -22,9 +22,8 @@ * commercial activities involving this program without disclosing the source * code of your own applications */ - -#include -#include +#include "kernel_stats.h" +#include "util/time.h" #if __linux__ #include @@ -38,7 +37,6 @@ namespace evcollect { namespace plugin_unix_stats { - bool getKernelInfo(KernelInfo kernel_info) { #if __linux__ struct sysinfo info; @@ -104,6 +102,18 @@ bool getKernelInfo(KernelInfo kernel_info) { kernel_info.uptime = (now.unixMicros() / kMicrosPerSecond) - t.tv_sec; } + /* kernel version */ + { + int mib[2] = {CTL_KERN, KERN_VERSION}; + char version[256]; + size_t len = 256; + if (sysctl(mib, 2, version, &len, NULL, 0) == -1) { + return false; + } + + printf("kernel version %s", version); + + } #endif return true; diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 8b5ca56..5390a61 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -27,10 +27,10 @@ #include #include #include -#include -#include -#include -#include +#include "disk_stats.h" +#include "kernel_stats.h" +#include "util/stringutil.h" +#include "util/time.h" #if __linux__ #include @@ -82,6 +82,7 @@ bool getProcessesEvent( std::ifstream file( StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), std::ifstream::in); + if (!file.is_open()) { return false; } @@ -165,7 +166,7 @@ bool getProcessesEvent( return false; } - struct kinfo_proc *info; + struct kinfo_proc* info; info = static_cast(malloc(len)); if (sysctl(mib, 3, info, &len, NULL, 0) == -1) { free(info); @@ -193,9 +194,9 @@ bool getProcessesEvent( pid, info[i].kp_eproc.e_ppid, info[i].kp_eproc.e_pgid, - info[i].kp_proc.p_nice, - info[i].kp_proc.p_un.__p_starttime.tv_sec, - info[i].kp_proc.p_stat)); + (uint64_t) info[i].kp_proc.p_nice, + (uint64_t) info[i].kp_proc.p_un.__p_starttime.tv_sec, + (uint64_t) info[i].kp_proc.p_stat)); } free(info); diff --git a/plugins/unix_stats/util/stringutil.cc b/plugins/unix_stats/util/stringutil.cc new file mode 120000 index 0000000..29b84bc --- /dev/null +++ b/plugins/unix_stats/util/stringutil.cc @@ -0,0 +1 @@ +../../../src/evcollect/util/stringutil.cc \ No newline at end of file diff --git a/plugins/unix_stats/util/stringutil.h b/plugins/unix_stats/util/stringutil.h new file mode 120000 index 0000000..11bea2f --- /dev/null +++ b/plugins/unix_stats/util/stringutil.h @@ -0,0 +1 @@ +../../../src/evcollect/util/stringutil.h \ No newline at end of file diff --git a/plugins/unix_stats/util/stringutil_impl.h b/plugins/unix_stats/util/stringutil_impl.h new file mode 120000 index 0000000..fb0d47e --- /dev/null +++ b/plugins/unix_stats/util/stringutil_impl.h @@ -0,0 +1 @@ +../../../src/evcollect/util/stringutil_impl.h \ No newline at end of file diff --git a/plugins/unix_stats/util/time.cc b/plugins/unix_stats/util/time.cc new file mode 120000 index 0000000..b18e128 --- /dev/null +++ b/plugins/unix_stats/util/time.cc @@ -0,0 +1 @@ +../../../src/evcollect/util/time.cc \ No newline at end of file diff --git a/plugins/unix_stats/util/time.h b/plugins/unix_stats/util/time.h new file mode 120000 index 0000000..70ac7c2 --- /dev/null +++ b/plugins/unix_stats/util/time.h @@ -0,0 +1 @@ +../../../src/evcollect/util/time.h \ No newline at end of file diff --git a/plugins/unix_stats/util/time_impl.h b/plugins/unix_stats/util/time_impl.h new file mode 120000 index 0000000..785f206 --- /dev/null +++ b/plugins/unix_stats/util/time_impl.h @@ -0,0 +1 @@ +../../../src/evcollect/util/time_impl.h \ No newline at end of file From 6301af0d09f35f719a7645cbd7ffc4050a57d3d5 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 14:00:27 +0200 Subject: [PATCH 59/78] fix unix stats plugin build --- plugins/unix_stats/disk_stats.h | 4 +- plugins/unix_stats/unix_stats_plugin.cc | 185 +++--------------------- 2 files changed, 20 insertions(+), 169 deletions(-) diff --git a/plugins/unix_stats/disk_stats.h b/plugins/unix_stats/disk_stats.h index f851d2d..4cdaaa8 100644 --- a/plugins/unix_stats/disk_stats.h +++ b/plugins/unix_stats/disk_stats.h @@ -46,9 +46,9 @@ struct DiskInfo { }; -bool getDiskInfo(DiskInfo disk_info); +bool getDiskInfo(std::vector disk_info); -std::string toJSON(DiskInfo disk_info); +std::string toJSON(std::vector disk_info); } //namespace plugin_unix_stats } //namespace evcollect diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 5390a61..b3229eb 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -51,177 +51,22 @@ namespace evcollect { namespace plugin_unix_stats { -bool getProcessesEvent( +int getEvent( evcollect_ctx_t* ctx, void* userdata, evcollect_event_t* ev) { - std::string evdata; -#if __linux__ - auto dir = opendir("/proc/"); - if (!dir) { - evcollect_seterror( - ctx, - "opendir failed"); - //StringUtil::format("opendir failed: $0", strerror(errno)).c_str()); - return false; - } - - evdata.append("["); - size_t i = 0; - for (;;) { - auto entry = readdir(dir); - if (!entry) { - break; - } - - if (entry->d_type != DT_DIR || (!StringUtil::isNumber(entry->d_name))) { - continue; - } - - std::ifstream file( - StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), - std::ifstream::in); - - if (!file.is_open()) { - return false; - } - - if (i++ > 0) { - evdata.append(","); - } - - evdata.append("{"); - - size_t j = 0; - std::string buf; - while (!file.eof()) { - char c; - file.get(c); - if (isspace(c)) { - switch (j++) { - case 0: /* process id */ - evdata.append(StringUtil::format(R"("pid": $0)", buf)); - break; - - case 1: /* filename of the executable */ - evdata.append(StringUtil::format(R"(,"name": "$0")", buf)); - break; - - case 2: /* process state */ - evdata.append(StringUtil::format(R"(,"state": "$0")", buf)); - break; - - case 3: /* parent PID */ - evdata.append(StringUtil::format(R"(,"ppid": $0)", buf)); - break; - - case 4: /* group ID */ - evdata.append(StringUtil::format(R"(,"pgrp": $0)", buf)); - break; - - case 13: /* time in user mode */ - evdata.append(StringUtil::format(R"(,"utime": $0)", buf)); - break; - - case 14: /* time in kernel mode */ - evdata.append(StringUtil::format(R"(,"stime": $0)", buf)); - break; - - case 18: /* nice value */ - evdata.append(StringUtil::format(R"(,"nice": $0)", buf)); - break; - - case 21: /* starttime */ - evdata.append(StringUtil::format(R"(,"starttime": $0)", buf)); - break; - - case 22: /* virtual memory size */ - evdata.append(StringUtil::format(R"(,"vsize": $0)", buf)); - break; - - default: - break; - } - - buf.clear(); - } else { - buf.push_back(c); - } - - } - - evdata.append("}"); - } - - evdata.append("]"); - -#elif __APPLE__ - size_t len = 0; - int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; - if (sysctl(mib, 3, NULL, &len, NULL, 0) == -1) { - evcollect_seterror( - ctx, - StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); - return false; - } - - struct kinfo_proc* info; - info = static_cast(malloc(len)); - if (sysctl(mib, 3, info, &len, NULL, 0) == -1) { - free(info); - evcollect_seterror( - ctx, - StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); - return false; - } - - int count = len / sizeof(kinfo_proc); - for (int i = 0; i < count; ++i) { - pid_t pid = info[i].kp_proc.p_pid; - if (pid == 0) { - continue; - } - - evdata.append(StringUtil::format( - R"({ - "pid": $0, - "parent": $1, - "group": $2, - "nice": $3, - "starttime": $4, - "state": "$5"})", - pid, - info[i].kp_eproc.e_ppid, - info[i].kp_eproc.e_pgid, - (uint64_t) info[i].kp_proc.p_nice, - (uint64_t) info[i].kp_proc.p_un.__p_starttime.tv_sec, - (uint64_t) info[i].kp_proc.p_stat)); - } - - free(info); -#endif - - evcollect_event_setdata(ev, evdata.data(), evdata.size()); - return true; -} - -bool getEvent( - evcollect_ctx_t* ctx, - void* userdata, - evcollect_event_t* ev) { - - DiskInfo disk_info; + std::vector disk_info; if (!getDiskInfo(disk_info)) { - return false; + return 0; } - KernelInfo kernel_info; - if (!getKernelInfo(kernel_info)) { - return false; - } + //plugin_unix_stats::KernelInfo kernel_info; + //if (!getKernelInfo(kernel_info)) { + // return false; + //} - return true; + return 1; // return (!getUptimeEvent(ctx, userdata, ev) || // !getLoadAvgEvent(ctx, userdata, ev) || // !getDiskUsageEvent(ctx, userdata, ev) || @@ -232,10 +77,16 @@ bool getEvent( } // namespace evcollect EVCOLLECT_PLUGIN_INIT(unix_stats) { - //evcollect_source_plugin_register( - // ctx, - // "unix.stats", - // &evcollect::plugin_unix_stats::getEvent); + evcollect_source_plugin_register( + ctx, + "unix.stats", + &evcollect::plugin_unix_stats::getEvent, + NULL, + NULL, + NULL, + NULL, + NULL); + //evcollect_source_plugin_register( // ctx, From d0dae99563a48af816a7bcdee423d0361eb13c9f Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 14:19:48 +0200 Subject: [PATCH 60/78] unix plugin: kernel version for osx --- plugins/unix_stats/kernel_stats.cc | 16 +++++++++++++--- plugins/unix_stats/kernel_stats.h | 4 +--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index 10dcdc5..0c9bd41 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -90,7 +90,6 @@ bool getKernelInfo(KernelInfo kernel_info) { { struct timeval t; size_t size = sizeof(t); - if (sysctlbyname("kern.boottime", &t, &size, NULL, 0) == -1) { //evcollect_seterror( // ctx, @@ -105,14 +104,25 @@ bool getKernelInfo(KernelInfo kernel_info) { /* kernel version */ { int mib[2] = {CTL_KERN, KERN_VERSION}; - char version[256]; size_t len = 256; + char version[len]; if (sysctl(mib, 2, version, &len, NULL, 0) == -1) { return false; } - printf("kernel version %s", version); + kernel_info.version = version; + } + + /* kernel version */ + { + int mib[2] = {CTL_KERN, KERN_VERSION}; + size_t len = 256; + char version[len]; + if (sysctl(mib, 2, version, &len, NULL, 0) == -1) { + return false; + } + kernel_info.version = version; } #endif diff --git a/plugins/unix_stats/kernel_stats.h b/plugins/unix_stats/kernel_stats.h index b40f2db..e8e77ec 100644 --- a/plugins/unix_stats/kernel_stats.h +++ b/plugins/unix_stats/kernel_stats.h @@ -40,9 +40,7 @@ struct KernelInfo { plugin_unix_stats::LoadAvg load_avg; std::string version; - std::string arguments; - std::string path; - std::string device; + std::string arguments; /* linux only */ }; From ec8aa65d22f36d6bbd448cd302598fef55688d5e Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 14:20:04 +0200 Subject: [PATCH 61/78] moved process stats code to own files --- plugins/unix_stats/process_stats.cc | 193 ++++++++++++++++++++++++ plugins/unix_stats/process_stats.h | 50 ++++++ plugins/unix_stats/unix_stats_plugin.cc | 8 +- 3 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 plugins/unix_stats/process_stats.cc create mode 100644 plugins/unix_stats/process_stats.h diff --git a/plugins/unix_stats/process_stats.cc b/plugins/unix_stats/process_stats.cc new file mode 100644 index 0000000..6e891ec --- /dev/null +++ b/plugins/unix_stats/process_stats.cc @@ -0,0 +1,193 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ + +#include "process_stats.h" + +namespace evcollect { +namespace plugin_unix_stats { + + +bool getProcessInfo(std::vector process_info) { +// std::string evdata; +// +//#if __linux__ +// auto dir = opendir("/proc/"); +// if (!dir) { +// evcollect_seterror( +// ctx, +// "opendir failed"); +// //StringUtil::format("opendir failed: $0", strerror(errno)).c_str()); +// return false; +// } +// +// evdata.append("["); +// size_t i = 0; +// for (;;) { +// auto entry = readdir(dir); +// if (!entry) { +// break; +// } +// +// if (entry->d_type != DT_DIR || (!StringUtil::isNumber(entry->d_name))) { +// continue; +// } +// +// std::ifstream file( +// StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), +// std::ifstream::in); +// +// if (!file.is_open()) { +// return false; +// } +// +// if (i++ > 0) { +// evdata.append(","); +// } +// +// evdata.append("{"); +// +// size_t j = 0; +// std::string buf; +// while (!file.eof()) { +// char c; +// file.get(c); +// if (isspace(c)) { +// switch (j++) { +// case 0: /* process id */ +// evdata.append(StringUtil::format(R"("pid": $0)", buf)); +// break; +// +// case 1: /* filename of the executable */ +// evdata.append(StringUtil::format(R"(,"name": "$0")", buf)); +// break; +// +// case 2: /* process state */ +// evdata.append(StringUtil::format(R"(,"state": "$0")", buf)); +// break; +// +// case 3: /* parent PID */ +// evdata.append(StringUtil::format(R"(,"ppid": $0)", buf)); +// break; +// +// case 4: /* group ID */ +// evdata.append(StringUtil::format(R"(,"pgrp": $0)", buf)); +// break; +// +// case 13: /* time in user mode */ +// evdata.append(StringUtil::format(R"(,"utime": $0)", buf)); +// break; +// +// case 14: /* time in kernel mode */ +// evdata.append(StringUtil::format(R"(,"stime": $0)", buf)); +// break; +// +// case 18: /* nice value */ +// evdata.append(StringUtil::format(R"(,"nice": $0)", buf)); +// break; +// +// case 21: /* starttime */ +// evdata.append(StringUtil::format(R"(,"starttime": $0)", buf)); +// break; +// +// case 22: /* virtual memory size */ +// evdata.append(StringUtil::format(R"(,"vsize": $0)", buf)); +// break; +// +// default: +// break; +// } +// +// buf.clear(); +// } else { +// buf.push_back(c); +// } +// +// } +// +// evdata.append("}"); +// } +// +// evdata.append("]"); +// +//#elif __APPLE__ +// size_t len = 0; +// int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; +// if (sysctl(mib, 3, NULL, &len, NULL, 0) == -1) { +// evcollect_seterror( +// ctx, +// StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); +// return false; +// } +// +// struct kinfo_proc* info; +// info = static_cast(malloc(len)); +// if (sysctl(mib, 3, info, &len, NULL, 0) == -1) { +// free(info); +// evcollect_seterror( +// ctx, +// StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); +// return false; +// } +// +// int count = len / sizeof(kinfo_proc); +// for (int i = 0; i < count; ++i) { +// pid_t pid = info[i].kp_proc.p_pid; +// if (pid == 0) { +// continue; +// } +// +// evdata.append(StringUtil::format( +// R"({ +// "pid": $0, +// "parent": $1, +// "group": $2, +// "nice": $3, +// "starttime": $4, +// "state": "$5"})", +// pid, +// info[i].kp_eproc.e_ppid, +// info[i].kp_eproc.e_pgid, +// (uint64_t) info[i].kp_proc.p_nice, +// (uint64_t) info[i].kp_proc.p_un.__p_starttime.tv_sec, +// (uint64_t) info[i].kp_proc.p_stat)); +// } +// +// free(info); +//#endif +// +// evcollect_event_setdata(ev, evdata.data(), evdata.size()); +// return true; + return true; +} + +std::string toJSON(std::vector process_info) { + std::string json; + return json; +} + +} //namespace plugin_unix_stats +} //namespace evcollect + + diff --git a/plugins/unix_stats/process_stats.h b/plugins/unix_stats/process_stats.h new file mode 100644 index 0000000..9523a40 --- /dev/null +++ b/plugins/unix_stats/process_stats.h @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2016 DeepCortex GmbH + * Authors: + * - Paul Asmuth + * - Laura Schlimmer + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License ("the license") as + * published by the Free Software Foundation, either version 3 of the License, + * or any later version. + * + * In accordance with Section 7(e) of the license, the licensing of the Program + * under the license does not imply a trademark license. Therefore any rights, + * title and interest in our trademarks remain entirely with us. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the license for more details. + * + * You can be released from the requirements of the license by purchasing a + * commercial license. Buying such a license is mandatory as soon as you develop + * commercial activities involving this program without disclosing the source + * code of your own applications + */ + +#include +#include + +namespace evcollect { +namespace plugin_unix_stats { + +struct ProcessInfo { + std::string filesystem; + std::string mount_point; + uint64_t total; + uint64_t used; + uint64_t available; + uint8_t capacity; + uint64_t iused; + uint64_t ifree; +}; + + +bool getProcessInfo(std::vector process_info); + +std::string toJSON(std::vector process_info); + +} //namespace plugin_unix_stats +} //namespace evcollect + diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index b3229eb..4643613 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -61,10 +61,10 @@ int getEvent( return 0; } - //plugin_unix_stats::KernelInfo kernel_info; - //if (!getKernelInfo(kernel_info)) { - // return false; - //} + plugin_unix_stats::KernelInfo kernel_info; + if (!getKernelInfo(kernel_info)) { + return false; + } return 1; // return (!getUptimeEvent(ctx, userdata, ev) || From bd0e0a71713906a59e2d077b5407b98d9f1b4f7b Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 15:11:27 +0200 Subject: [PATCH 62/78] unix stats plugin: kernel version for linux --- plugins/unix_stats/kernel_stats.cc | 43 ++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index 0c9bd41..a4668f7 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -27,6 +27,7 @@ #if __linux__ #include +#include #endif #if __APPLE__ @@ -39,25 +40,39 @@ namespace plugin_unix_stats { bool getKernelInfo(KernelInfo kernel_info) { #if __linux__ - struct sysinfo info; - if (sysinfo(&info) == -1) { - //evcollect_seterror(ctx, "sysinfo failed"); - return false; - } + /* load averages and uptime */ + { + struct sysinfo info; + if (sysinfo(&info) == -1) { + //evcollect_seterror(ctx, "sysinfo failed"); + return false; + } + + /* load average for the last 1, 5 and 15 minutes */ + switch (sizeof(info.loads) / sizeof(info.loads[0])) { + case 3: + kernel_info.load_avg.min15 = info.loads[2]; + + case 2: + kernel_info.load_avg.min15 = info.loads[1]; + + case 1: + kernel_info.load_avg.min5 = info.loads[0]; + } - /* load average for the last 1, 5 and 15 minutes */ - switch (sizeof(info.loads) / sizeof(info.loads[0])) { - case 3: - kernel_info.load_avg.min15 = info_loads[2]; + kernel_info.uptime = info.uptime; + } - case 2: - kernel_info.load_avg.min15 = info_loads[1]; + /* version */ + { + struct utsname info; + if (uname(&info) == -1) { + return false; + } - case 1: - kernel_info.load_avg.min5 = info_loads[0]; + kernel_info.version = info.version; } - kernel_info.uptime = uptime; #elif __APPLE__ From 30e9ea0381546f275b3a27650a0367f99a07a36b Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 15:24:30 +0200 Subject: [PATCH 63/78] unix stats plugin: linux kernel arguments --- plugins/unix_stats/kernel_stats.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index a4668f7..a6cf5b3 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -73,7 +73,20 @@ bool getKernelInfo(KernelInfo kernel_info) { kernel_info.version = info.version; } + /* arguments */ + { + auto file = fopen("/proc/cmdline", "r"); + if (!file) { + return false; + } + char buf[256]; //FIXME size + while (fgets(buf, sizeof(buf), file)) { + kernel_info.arguments.append(buf); + } + + fclose(file); + } #elif __APPLE__ /* load averages */ From 6b5a389accf07a601158375d41d821d570a07355 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 15:24:46 +0200 Subject: [PATCH 64/78] don't use logging in time --- src/evcollect/util/time.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/evcollect/util/time.cc b/src/evcollect/util/time.cc index c2e54fd..109eee2 100644 --- a/src/evcollect/util/time.cc +++ b/src/evcollect/util/time.cc @@ -78,7 +78,7 @@ uint64_t MonotonicClock::now() { #else timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { - logFatal("clock_gettime(CLOCK_MONOTONIC) failed"); + //logFatal("clock_gettime(CLOCK_MONOTONIC) failed"); abort(); } else { return std::uint64_t(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000; From 6d208a38e9c658cb4d6bef9ee479055b188fbbeb Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 15:47:56 +0200 Subject: [PATCH 65/78] comments --- plugins/unix_stats/kernel_stats.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index a6cf5b3..f43a44a 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -63,7 +63,7 @@ bool getKernelInfo(KernelInfo kernel_info) { kernel_info.uptime = info.uptime; } - /* version */ + /* kernel version */ { struct utsname info; if (uname(&info) == -1) { @@ -73,7 +73,7 @@ bool getKernelInfo(KernelInfo kernel_info) { kernel_info.version = info.version; } - /* arguments */ + /* kernel arguments */ { auto file = fopen("/proc/cmdline", "r"); if (!file) { From f6cef96bc4fce2b25818a6f021cfcdd78a1036ca Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 15:48:15 +0200 Subject: [PATCH 66/78] unix stats plugin: getProcessInfo --- plugins/unix_stats/Makefile.am | 2 + plugins/unix_stats/process_stats.cc | 284 +++++++++++------------- plugins/unix_stats/process_stats.h | 19 +- plugins/unix_stats/unix_stats_plugin.cc | 8 +- 4 files changed, 154 insertions(+), 159 deletions(-) diff --git a/plugins/unix_stats/Makefile.am b/plugins/unix_stats/Makefile.am index 816fd2d..e7f5945 100644 --- a/plugins/unix_stats/Makefile.am +++ b/plugins/unix_stats/Makefile.am @@ -17,6 +17,8 @@ plugin_unix_stats_la_SOURCES = \ disk_stats.cc \ kernel_stats.h \ kernel_stats.cc \ + process_stats.h \ + process_stats.cc \ unix_stats_plugin.cc PLUGINDIR=$(DESTDIR)$(libdir)/evcollect/plugins diff --git a/plugins/unix_stats/process_stats.cc b/plugins/unix_stats/process_stats.cc index 6e891ec..4655841 100644 --- a/plugins/unix_stats/process_stats.cc +++ b/plugins/unix_stats/process_stats.cc @@ -24,161 +24,147 @@ */ #include "process_stats.h" +#if __linux__ +//#include +//#include +//#include +//#include +#endif + +#if __APPLE__ +#include +#endif namespace evcollect { namespace plugin_unix_stats { bool getProcessInfo(std::vector process_info) { -// std::string evdata; -// -//#if __linux__ -// auto dir = opendir("/proc/"); -// if (!dir) { -// evcollect_seterror( -// ctx, -// "opendir failed"); -// //StringUtil::format("opendir failed: $0", strerror(errno)).c_str()); -// return false; -// } -// -// evdata.append("["); -// size_t i = 0; -// for (;;) { -// auto entry = readdir(dir); -// if (!entry) { -// break; -// } -// -// if (entry->d_type != DT_DIR || (!StringUtil::isNumber(entry->d_name))) { -// continue; -// } -// -// std::ifstream file( -// StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), -// std::ifstream::in); -// -// if (!file.is_open()) { -// return false; -// } -// -// if (i++ > 0) { -// evdata.append(","); -// } -// -// evdata.append("{"); -// -// size_t j = 0; -// std::string buf; -// while (!file.eof()) { -// char c; -// file.get(c); -// if (isspace(c)) { -// switch (j++) { -// case 0: /* process id */ -// evdata.append(StringUtil::format(R"("pid": $0)", buf)); -// break; -// -// case 1: /* filename of the executable */ -// evdata.append(StringUtil::format(R"(,"name": "$0")", buf)); -// break; -// -// case 2: /* process state */ -// evdata.append(StringUtil::format(R"(,"state": "$0")", buf)); -// break; -// -// case 3: /* parent PID */ -// evdata.append(StringUtil::format(R"(,"ppid": $0)", buf)); -// break; -// -// case 4: /* group ID */ -// evdata.append(StringUtil::format(R"(,"pgrp": $0)", buf)); -// break; -// -// case 13: /* time in user mode */ -// evdata.append(StringUtil::format(R"(,"utime": $0)", buf)); -// break; -// -// case 14: /* time in kernel mode */ -// evdata.append(StringUtil::format(R"(,"stime": $0)", buf)); -// break; -// -// case 18: /* nice value */ -// evdata.append(StringUtil::format(R"(,"nice": $0)", buf)); -// break; -// -// case 21: /* starttime */ -// evdata.append(StringUtil::format(R"(,"starttime": $0)", buf)); -// break; -// -// case 22: /* virtual memory size */ -// evdata.append(StringUtil::format(R"(,"vsize": $0)", buf)); -// break; -// -// default: -// break; -// } -// -// buf.clear(); -// } else { -// buf.push_back(c); -// } -// -// } -// -// evdata.append("}"); -// } -// -// evdata.append("]"); -// -//#elif __APPLE__ -// size_t len = 0; -// int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; -// if (sysctl(mib, 3, NULL, &len, NULL, 0) == -1) { -// evcollect_seterror( -// ctx, -// StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); -// return false; -// } -// -// struct kinfo_proc* info; -// info = static_cast(malloc(len)); -// if (sysctl(mib, 3, info, &len, NULL, 0) == -1) { -// free(info); -// evcollect_seterror( -// ctx, -// StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); -// return false; -// } -// -// int count = len / sizeof(kinfo_proc); -// for (int i = 0; i < count; ++i) { -// pid_t pid = info[i].kp_proc.p_pid; -// if (pid == 0) { -// continue; -// } -// -// evdata.append(StringUtil::format( -// R"({ -// "pid": $0, -// "parent": $1, -// "group": $2, -// "nice": $3, -// "starttime": $4, -// "state": "$5"})", -// pid, -// info[i].kp_eproc.e_ppid, -// info[i].kp_eproc.e_pgid, -// (uint64_t) info[i].kp_proc.p_nice, -// (uint64_t) info[i].kp_proc.p_un.__p_starttime.tv_sec, -// (uint64_t) info[i].kp_proc.p_stat)); -// } -// -// free(info); -//#endif -// -// evcollect_event_setdata(ev, evdata.data(), evdata.size()); -// return true; +#if __linux__ + auto dir = opendir("/proc/"); + if (!dir) { + // //StringUtil::format("opendir failed: $0", strerror(errno)).c_str()); + return false; + } + + for (;;) { + auto entry = readdir(dir); + if (!entry) { + break; + } + + if (entry->d_type != DT_DIR || (!StringUtil::isNumber(entry->d_name))) { + continue; + } + + std::ifstream file( + StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), + std::ifstream::in); + + if (!file.is_open()) { + return false; + } + + size_t i = 0; + std::string buf; + ProcessInfo info; + while (!file.eof()) { + char c; + file.get(c); + if (isspace(c)) { + switch (i++) { + case 0: /* process id */ + info.pid = buf; + break; + + case 1: /* filename of the executable */ + info.name = buf; + break; + + case 2: /* process state */ + info.state = buf; + break; + + case 3: /* parent PID */ + info.ppid = buf; + break; + + case 4: /* group ID */ + info.pgrp = buf; + break; + + case 13: /* time in user mode */ + info.utime = buf; + break; + + case 14: /* time in kernel mode */ + info.stime - buf; + break; + + case 18: /* nice value */ + info.nice = buf; + break; + + case 21: /* starttime */ + info.starttime = buf; + break; + + case 22: /* virtual memory size */ + info.vsize = buf; + break; + + default: + break; + } + + buf.clear(); + } else { + buf.push_back(c); + } + } + + process_info.emplace_back(info); + } + + closedir(dir); + +#elif __APPLE__ + size_t len = 0; + int name[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; + /* size of kinfo_proc */ + if (sysctl(name, 3, NULL, &len, NULL, 0) == -1) { + // StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); + return false; + } + + struct kinfo_proc* info; + info = static_cast(malloc(len)); + if (sysctl(name, 3, info, &len, NULL, 0) == -1) { + free(info); + // StringUtil::format("sysctl failed: $0", strerror(errno)).c_str()); + return false; + } + + int count = len / sizeof(kinfo_proc); + for (int i = 0; i < count; ++i) { + pid_t pid = info[i].kp_proc.p_pid; + if (pid == 0) { + continue; + } + + struct ProcessInfo pinfo; + pinfo.pid = pid; + pinfo.ppid = info[i].kp_eproc.e_ppid; + pinfo.pgrp = info[i].kp_eproc.e_pgid; + pinfo.nice = (uint64_t) info[i].kp_proc.p_nice; + pinfo.starttime = (uint64_t) info[i].kp_proc.p_un.__p_starttime.tv_sec; + pinfo.state = info[i].kp_proc.p_stat; + + process_info.emplace_back(pinfo); + } + + free(info); +#endif return true; } diff --git a/plugins/unix_stats/process_stats.h b/plugins/unix_stats/process_stats.h index 9523a40..745f307 100644 --- a/plugins/unix_stats/process_stats.h +++ b/plugins/unix_stats/process_stats.h @@ -30,17 +30,18 @@ namespace evcollect { namespace plugin_unix_stats { struct ProcessInfo { - std::string filesystem; - std::string mount_point; - uint64_t total; - uint64_t used; - uint64_t available; - uint8_t capacity; - uint64_t iused; - uint64_t ifree; + uint64_t pid; + std::string name; + std::string state; + uint64_t ppid; + uint64_t pgrp; + uint64_t utime; + uint64_t stime; + int8_t nice; + uint64_t starttime; + uint64_t vsize; }; - bool getProcessInfo(std::vector process_info); std::string toJSON(std::vector process_info); diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index 4643613..ee1ab0f 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -29,6 +29,7 @@ #include #include "disk_stats.h" #include "kernel_stats.h" +#include "process_stats.h" #include "util/stringutil.h" #include "util/time.h" @@ -61,11 +62,16 @@ int getEvent( return 0; } - plugin_unix_stats::KernelInfo kernel_info; + KernelInfo kernel_info; if (!getKernelInfo(kernel_info)) { return false; } + std::vector process_list; + if (!getProcessInfo(process_list)) { + return false; + } + return 1; // return (!getUptimeEvent(ctx, userdata, ev) || // !getLoadAvgEvent(ctx, userdata, ev) || From d4b931e767b154e5b0335e19b965ece3b8306fcd Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 12 Aug 2016 15:55:37 +0200 Subject: [PATCH 67/78] make sure all open directories are closed --- plugins/unix_stats/process_stats.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/unix_stats/process_stats.cc b/plugins/unix_stats/process_stats.cc index 4655841..9f5e435 100644 --- a/plugins/unix_stats/process_stats.cc +++ b/plugins/unix_stats/process_stats.cc @@ -49,10 +49,12 @@ bool getProcessInfo(std::vector process_info) { for (;;) { auto entry = readdir(dir); + /* end of directory */ if (!entry) { break; } + /* skip if not one of the /proc/{pid} directories */ if (entry->d_type != DT_DIR || (!StringUtil::isNumber(entry->d_name))) { continue; } @@ -124,6 +126,7 @@ bool getProcessInfo(std::vector process_info) { } process_info.emplace_back(info); + closedir(entry); } closedir(dir); From 66090053795fc1187e81181f1f5527bf39c4fdca Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 14:21:52 +0200 Subject: [PATCH 68/78] kernel info json --- plugins/unix_stats/kernel_stats.cc | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index f43a44a..9c9e1f5 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -24,6 +24,7 @@ */ #include "kernel_stats.h" #include "util/time.h" +#include "util/stringutil.h" #if __linux__ #include @@ -158,7 +159,25 @@ bool getKernelInfo(KernelInfo kernel_info) { } std::string toJSON(KernelInfo kernel_info) { - + std::string json = StringUtil::format(R"({ + "kernel": { + "uptime": $0, + "load_avg": { + "min1": $1, + "min5": $2, + "min15": $3 + }, + "version": $4, + "arguments": $5 + }})", + kernel_info.uptime, + kernel_info.load_avg.min1, + kernel_info.load_avg.min5, + kernel_info.load_avg.min15, + kernel_info.version, + kernel_info.arguments); + + return json; } } //namespace plugin_unix_stats From 502be9f6418462fbc0ab2c9dffc7f04ab6618985 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 16:00:54 +0200 Subject: [PATCH 69/78] cleanup --- plugins/unix_stats/unix_stats_plugin.cc | 33 ++++++------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index ee1ab0f..f59ea39 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -64,19 +64,20 @@ int getEvent( KernelInfo kernel_info; if (!getKernelInfo(kernel_info)) { - return false; + return 0; } std::vector process_list; if (!getProcessInfo(process_list)) { - return false; + return 0; } + std::string json; + json.append(toJSON(disk_info)); + json.append(toJSON(kernel_info)); + json.append(toJSON(process_list)); + return 1; - // return (!getUptimeEvent(ctx, userdata, ev) || - // !getLoadAvgEvent(ctx, userdata, ev) || - // !getDiskUsageEvent(ctx, userdata, ev) || - // !getProcessesEvent(ctx, userdata, ev)); } } // namespace plugin_unix_stats @@ -92,26 +93,6 @@ EVCOLLECT_PLUGIN_INIT(unix_stats) { NULL, NULL, NULL); - - - //evcollect_source_plugin_register( - // ctx, - // "unix.uptime", - // &evcollect::plugin_unix_stats::getUptimeEvent); - - //evcollect_source_plugin_register( - // ctx, - // "unix.load_avg", - // &evcollect::plugin_unix_stats::getLoadAvgEvent); - - //evcollect_source_plugin_register( - // ctx, - // "unix.disk_usage", - // &evcollect::plugin_unix_stats::getDiskUsageEvent); - //evcollect_source_plugin_register( - // ctx, - // "unix.processes", - // &evcollect::plugin_unix_stats::getProcessesEvent); return true; } From 1a06e689b5941c61ce83bd659d686e9ec798fe9f Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 16:09:14 +0200 Subject: [PATCH 70/78] {kernel, process, disk} stats json --- plugins/unix_stats/disk_stats.cc | 14 ++++++----- plugins/unix_stats/kernel_stats.cc | 4 +-- plugins/unix_stats/process_stats.cc | 39 +++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc index 64fc85a..412f68e 100644 --- a/plugins/unix_stats/disk_stats.cc +++ b/plugins/unix_stats/disk_stats.cc @@ -107,12 +107,14 @@ std::string toJSON(std::vector disk_info) { } json.append(StringUtil::format(R"({ - "total": $0, - "available": $1, - "used": $2, - "capacity": $3, - "ifree": $4, - "iused": $5})", + "disk": { + "total": $0, + "available": $1, + "used": $2, + "capacity": $3, + "ifree": $4, + "iused": $5 + }})", disk_info[i].total, disk_info[i].available, disk_info[i].used, diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index 9c9e1f5..68b2bbe 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -167,8 +167,8 @@ std::string toJSON(KernelInfo kernel_info) { "min5": $2, "min15": $3 }, - "version": $4, - "arguments": $5 + "version": "$4", + "arguments": "$5" }})", kernel_info.uptime, kernel_info.load_avg.min1, diff --git a/plugins/unix_stats/process_stats.cc b/plugins/unix_stats/process_stats.cc index 9f5e435..e4855c9 100644 --- a/plugins/unix_stats/process_stats.cc +++ b/plugins/unix_stats/process_stats.cc @@ -23,6 +23,7 @@ * code of your own applications */ +#include "util/stringutil.h" #include "process_stats.h" #if __linux__ //#include @@ -171,8 +172,42 @@ bool getProcessInfo(std::vector process_info) { return true; } -std::string toJSON(std::vector process_info) { - std::string json; +std::string toJSON(std::vector process_list) { + std::string json = "["; + + for (size_t i = 0; i < process_list.size(); ++i) { + if (i > 0) { + json.append(","); + } + + + json.append(StringUtil::format(R"({ + "process": { + "pid": $0, + "name": "$1", + "state": "$2", + "ppid": $3, + "pgrp": $4, + "utime": $5, + "stime": $6, + "nice": $7, + "starttime": $8, + "vsize": $9, + }})", + process_list[i].pid, + process_list[i].name, + process_list[i].state, + process_list[i].ppid, + process_list[i].pgrp, + process_list[i].utime, + process_list[i].stime, + process_list[i].nice, + process_list[i].starttime, + process_list[i].vsize)); + } + + json.append("]"); + return json; } From 8a6b3aa050c96aac3bd6f6aeed3db6e73a0635ed Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 16:27:47 +0200 Subject: [PATCH 71/78] disk stats: close mount info file --- plugins/unix_stats/disk_stats.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc index 412f68e..0c27450 100644 --- a/plugins/unix_stats/disk_stats.cc +++ b/plugins/unix_stats/disk_stats.cc @@ -54,7 +54,7 @@ std::vector getMountInfo() { mount_info.emplace_back(mn_info); } - //FIXME close file + fclose(file); #elif __APPLE__ struct statfs* mntbuf; From a0115ebe5a7795447f6e3ea525e6db86d81ee875 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 16:28:53 +0200 Subject: [PATCH 72/78] process stats: linux fixes --- plugins/unix_stats/process_stats.cc | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/plugins/unix_stats/process_stats.cc b/plugins/unix_stats/process_stats.cc index 9f5e435..16d6acd 100644 --- a/plugins/unix_stats/process_stats.cc +++ b/plugins/unix_stats/process_stats.cc @@ -24,10 +24,11 @@ */ #include "process_stats.h" +#include "util/stringutil.h" #if __linux__ -//#include +#include //#include -//#include +#include //#include #endif @@ -59,8 +60,9 @@ bool getProcessInfo(std::vector process_info) { continue; } + auto file_name = StringUtil::format("/proc/$0/stat", entry->d_name); std::ifstream file( - StringUtil::format("/proc/$0/stat", entry->d_name).c_str(), + file_name.c_str(), std::ifstream::in); if (!file.is_open()) { @@ -76,7 +78,7 @@ bool getProcessInfo(std::vector process_info) { if (isspace(c)) { switch (i++) { case 0: /* process id */ - info.pid = buf; + info.pid = std::stoull(buf); break; case 1: /* filename of the executable */ @@ -88,31 +90,31 @@ bool getProcessInfo(std::vector process_info) { break; case 3: /* parent PID */ - info.ppid = buf; + info.ppid = std::stoull(buf); break; case 4: /* group ID */ - info.pgrp = buf; + info.pgrp = std::stoull(buf); break; case 13: /* time in user mode */ - info.utime = buf; + info.utime = std::stoull(buf); break; case 14: /* time in kernel mode */ - info.stime - buf; + info.stime = std::stoull(buf); break; case 18: /* nice value */ - info.nice = buf; + info.nice = std::stoul(buf); break; case 21: /* starttime */ - info.starttime = buf; + info.starttime = std::stoull(buf); break; case 22: /* virtual memory size */ - info.vsize = buf; + info.vsize = std::stoull(buf); break; default: @@ -126,7 +128,6 @@ bool getProcessInfo(std::vector process_info) { } process_info.emplace_back(info); - closedir(entry); } closedir(dir); From 949b0b30ec05f8d78324c0b1cb10c462b9533d09 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 16:58:11 +0200 Subject: [PATCH 73/78] cleanup --- plugins/unix_stats/unix_stats_plugin.cc | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.cc b/plugins/unix_stats/unix_stats_plugin.cc index f59ea39..61f3014 100644 --- a/plugins/unix_stats/unix_stats_plugin.cc +++ b/plugins/unix_stats/unix_stats_plugin.cc @@ -23,31 +23,10 @@ * code of your own applications */ #include -#include -#include -#include #include #include "disk_stats.h" #include "kernel_stats.h" #include "process_stats.h" -#include "util/stringutil.h" -#include "util/time.h" - -#if __linux__ -#include -#include -#include -#include -#endif - -#if __APPLE__ -#include -#include -#include -#include -#include -#include -#endif namespace evcollect { namespace plugin_unix_stats { From c5a13737e945c3dfb045a7f7cc16b702f1033972 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Mon, 15 Aug 2016 16:58:59 +0200 Subject: [PATCH 74/78] cleanup --- plugins/unix_stats/disk_stats.cc | 2 +- plugins/unix_stats/disk_stats.h | 1 - plugins/unix_stats/process_stats.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc index 0c27450..0eb3893 100644 --- a/plugins/unix_stats/disk_stats.cc +++ b/plugins/unix_stats/disk_stats.cc @@ -26,7 +26,7 @@ #include #include #include -#include +#include "util/stringutil.h" #include "disk_stats.h" #if __linux__ diff --git a/plugins/unix_stats/disk_stats.h b/plugins/unix_stats/disk_stats.h index 4cdaaa8..76e072d 100644 --- a/plugins/unix_stats/disk_stats.h +++ b/plugins/unix_stats/disk_stats.h @@ -24,7 +24,6 @@ */ #include -#include namespace evcollect { namespace plugin_unix_stats { diff --git a/plugins/unix_stats/process_stats.h b/plugins/unix_stats/process_stats.h index 745f307..1cd9d58 100644 --- a/plugins/unix_stats/process_stats.h +++ b/plugins/unix_stats/process_stats.h @@ -24,7 +24,6 @@ */ #include -#include namespace evcollect { namespace plugin_unix_stats { From f00b3c51f0322c9c747eaa691f223203e6e92cb2 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 19 Aug 2016 17:23:48 +0200 Subject: [PATCH 75/78] docu --- plugins/unix_stats/unix_stats_plugin.md | 117 ++++++++++-------------- 1 file changed, 50 insertions(+), 67 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.md b/plugins/unix_stats/unix_stats_plugin.md index b1cb5b0..3c71a00 100644 --- a/plugins/unix_stats/unix_stats_plugin.md +++ b/plugins/unix_stats/unix_stats_plugin.md @@ -1,29 +1,41 @@ # evcollect unix.stats +different plugins that can each used by itself or combined + ## Tables -###unix.uptime +###unix.kernel - + - + - + - + - + - + - + - + + + + + + + + + + +
daysuptime IntegerDays of uptimeSeconds of uptime
hoursload_avg.min1 IntegerHours of uptimeLoad average during the last minute
minutesload_avg.min5 IntegerMinutes of uptimeLoad average during the last five minutes
secondsload_avg.min15 IntegerSeconds of uptimeLoad average during the last 15 minutes
argumentsStringKernel arguments
versionIntegerKernel version
@@ -33,126 +45,97 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - +
disk_stats.filesystemdisk.filesystem StringThe name of the file system.The name of the file system
disk_stats.mount_pointdisk.mount_point StringThe mount point.The mount point
disk_stats.totaldisk.total IntegerThe total size of the file system in bytes.The total size of the file system in bytes
disk_stats.useddisk.available IntegerThe total space used in the file system in bytesThe total free space in bytes
disk_stats.availabledisk.used IntegerThe total free space in bytes.The total space used in the file system in bytes
disk_stats.capacitydisk.capacity IntegerThe percentage of space that is used between 0 and 100.The percentage of space that is used between 0 and 100
disk_stats.iuseddisk.iused IntegerThe number of used inodes.The number of used inodes
disk_stats.ifreedisk.ifree IntegerThe number of free inodes.The number of free inodes
-###unix.load_avg - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
load_avgRepeated record
load_avg.minIntegerOne of 1, 5 or 15
load_avg.valueDoubleThe load average
freeramIntegerThe size of available memory.
freeswapIntegerThe size of available swap space.
- ###unix.processes - - - - - - - - - - -
process.pidThe process ID. + The process ID
process.nameThe filename of the executable. + The filename of the executable
process.stateThe process state. + The process state
process.ppidThe parent PID of the process. + The parent PID of the process
process.pgrpThe group ID of the process. + The group ID of the process
process.ppidThe PID of the parent of the process. + The PID of the parent of the process
process.utimeThe time, measured in clock ticks, spent the process spent in user mode. + The time, measured in clock ticks, spent the process spent in user mode
process.stimeThe time, measured in clock ticks, spent the process spent in kernel mode. + The time, measured in clock ticks, spent the process spent in kernel mode
process.niceThe process nice level (-20 - 19). + The process nice level (-20 - 19)
process.starttimeThe time the process started after system boot. + The time the process started after system boot
process.vsizeThe virtual memory size in bytes. + The virtual memory size in bytes
+ + From 850f582285365a5fe3b5b8418969d2aaf0c439f1 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 19 Aug 2016 17:39:02 +0200 Subject: [PATCH 76/78] docu --- plugins/unix_stats/unix_stats_plugin.md | 59 +++++++++++++++++++++---- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/plugins/unix_stats/unix_stats_plugin.md b/plugins/unix_stats/unix_stats_plugin.md index 3c71a00..0f447a2 100644 --- a/plugins/unix_stats/unix_stats_plugin.md +++ b/plugins/unix_stats/unix_stats_plugin.md @@ -14,17 +14,17 @@ different plugins that can each used by itself or combined load_avg.min1 - Integer + Double Load average during the last minute load_avg.min5 - Integer + Double Load average during the last five minutes load_avg.min15 - Integer + Double Load average during the last 15 minutes @@ -41,6 +41,13 @@ different plugins that can each used by itself or combined + CREATE TABLE unix.kernel ( + uptime UINT64, + load_avg RECORD (min1, min5, min15), + arguments STRING, + version STRING + ) + ###unix.disk_usage @@ -88,54 +95,88 @@ different plugins that can each used by itself or combined
+ CREATE TABLE unix.disk ( + disk RECORD ( + filesystem STRING, + mount_point STRING, + total UINT64, + available UINT64, + used UINT64, + capacity UINT32, + iused UINT64, + ifree UINT64 + ) + ) + ###unix.processes + + + + + - - - - + - + + + +
process.pidInteger The process ID
process.nameString The filename of the executable
process.stateString The process state
process.ppidInteger The parent PID of the process
process.pgrpInteger The group ID of the process
process.ppidThe PID of the parent of the process -
process.utimeThe time, measured in clock ticks, spent the process spent in user mode + IntegerThe time, measured in clock ticks, the process spent in user mode
process.stimeThe time, measured in clock ticks, spent the process spent in kernel mode + IntegerThe time, measured in clock ticks, the process spent in kernel mode
process.niceDouble The process nice level (-20 - 19)
process.starttimeInteger The time the process started after system boot
process.vsizeInteger The virtual memory size in bytes
+ CREATE TABLE unix.process ( + process RECORD ( + pid UINT64, + name STRING, + state STRING, + ppid UINT64, + pgrp UINT64, + utime UINT64, + stime UINT64, + nice DOUBLE, + starttime UINT64, + vsize UINT64 + ) + ) + From 6899de939b3466f65cffe33cd433e1b79727be3c Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 19 Aug 2016 17:39:33 +0200 Subject: [PATCH 77/78] unix stats event json --- plugins/unix_stats/disk_stats.cc | 25 ++++++++++++++----------- plugins/unix_stats/kernel_stats.cc | 19 +++++++++---------- plugins/unix_stats/process_stats.cc | 9 ++++----- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/plugins/unix_stats/disk_stats.cc b/plugins/unix_stats/disk_stats.cc index 0eb3893..32282c2 100644 --- a/plugins/unix_stats/disk_stats.cc +++ b/plugins/unix_stats/disk_stats.cc @@ -99,22 +99,25 @@ bool getDiskInfo(std::vector disk_info) { std::string toJSON(std::vector disk_info) { std::string json; - json.append(R"([)"); + json.append(R"({disk: )"); for (size_t i = 0; i < disk_info.size(); ++i) { if (i > 0) { json.append(","); } - json.append(StringUtil::format(R"({ - "disk": { - "total": $0, - "available": $1, - "used": $2, - "capacity": $3, - "ifree": $4, - "iused": $5 - }})", + json.append(StringUtil::format(R"([ + "filesystem": $0, + "mount_point": $1, + "total": $2, + "available": $3, + "used": $4, + "capacity": $5, + "ifree": $6, + "iused": $7 + ])", + disk_info[i].filesystem, + disk_info[i].mount_point, disk_info[i].total, disk_info[i].available, disk_info[i].used, @@ -124,7 +127,7 @@ std::string toJSON(std::vector disk_info) { )); } - json.append("]"); + json.append("}"); return json; } diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index 68b2bbe..8399102 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -160,16 +160,15 @@ bool getKernelInfo(KernelInfo kernel_info) { std::string toJSON(KernelInfo kernel_info) { std::string json = StringUtil::format(R"({ - "kernel": { - "uptime": $0, - "load_avg": { - "min1": $1, - "min5": $2, - "min15": $3 - }, - "version": "$4", - "arguments": "$5" - }})", + "uptime": $0, + "load_avg": { + "min1": $1, + "min5": $2, + "min15": $3 + }, + "version": "$4", + "arguments": "$5" + })", kernel_info.uptime, kernel_info.load_avg.min1, kernel_info.load_avg.min5, diff --git a/plugins/unix_stats/process_stats.cc b/plugins/unix_stats/process_stats.cc index 60fd4ae..24f8c98 100644 --- a/plugins/unix_stats/process_stats.cc +++ b/plugins/unix_stats/process_stats.cc @@ -174,7 +174,7 @@ bool getProcessInfo(std::vector process_info) { } std::string toJSON(std::vector process_list) { - std::string json = "["; + std::string json = "{process: "; for (size_t i = 0; i < process_list.size(); ++i) { if (i > 0) { @@ -182,8 +182,7 @@ std::string toJSON(std::vector process_list) { } - json.append(StringUtil::format(R"({ - "process": { + json.append(StringUtil::format(R"([ "pid": $0, "name": "$1", "state": "$2", @@ -194,7 +193,7 @@ std::string toJSON(std::vector process_list) { "nice": $7, "starttime": $8, "vsize": $9, - }})", + ])", process_list[i].pid, process_list[i].name, process_list[i].state, @@ -207,7 +206,7 @@ std::string toJSON(std::vector process_list) { process_list[i].vsize)); } - json.append("]"); + json.append("}"); return json; } From b6d6c99a10b9030fc581507b341564a5494fe956 Mon Sep 17 00:00:00 2001 From: Laura Schlimmer Date: Fri, 19 Aug 2016 17:44:41 +0200 Subject: [PATCH 78/78] includes --- plugins/unix_stats/kernel_stats.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/unix_stats/kernel_stats.cc b/plugins/unix_stats/kernel_stats.cc index 8399102..86b092a 100644 --- a/plugins/unix_stats/kernel_stats.cc +++ b/plugins/unix_stats/kernel_stats.cc @@ -23,7 +23,6 @@ * code of your own applications */ #include "kernel_stats.h" -#include "util/time.h" #include "util/stringutil.h" #if __linux__ @@ -32,6 +31,7 @@ #endif #if __APPLE__ +#include "util/time.h" #include #endif