mirror of
https://github.com/openappsec/openappsec.git
synced 2025-11-19 10:34:26 +03:00
sync code
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include "debug.h"
|
||||
#include "report/log_rest.h"
|
||||
#include "config.h"
|
||||
#include "report_messaging.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@@ -30,6 +31,24 @@ MetricMetadata::DotName operator"" _dot(const char *str, size_t) { return Metric
|
||||
MetricMetadata::Units operator"" _unit(const char *str, size_t) { return MetricMetadata::Units{str}; }
|
||||
MetricMetadata::Description operator"" _desc(const char *str, size_t) { return MetricMetadata::Description{str}; }
|
||||
|
||||
// LCOV_EXCL_START Reason: Tested in unit test (testAIOPSMapMetric), but not detected by coverage
|
||||
static ostream & operator<<(ostream &os, const AiopsMetricList &metrics) { return os << metrics.toString(); }
|
||||
// LCOV_EXCL_STOP
|
||||
|
||||
vector<AiopsMetricData>
|
||||
MetricCalc::getAiopsMetrics() const
|
||||
{
|
||||
float value = getValue();
|
||||
if (isnan(value)) return {};
|
||||
|
||||
string name = getMetricDotName() != "" ? getMetricDotName() : getMetricName();
|
||||
string units = getMetircUnits();
|
||||
string description = getMetircDescription();
|
||||
string type = getMetricType() == MetricType::GAUGE ? "gauge" : "counter";
|
||||
|
||||
return { AiopsMetricData(name, type, units, description, getBasicLabels(), value) };
|
||||
}
|
||||
|
||||
string
|
||||
MetricCalc::getMetadata(const string &key) const
|
||||
{
|
||||
@@ -54,6 +73,54 @@ MetricCalc::addMetric(GenericMetric *metric)
|
||||
if (metric != nullptr) metric->addCalc(this);
|
||||
}
|
||||
|
||||
vector<PrometheusData>
|
||||
MetricCalc::getPrometheusMetrics() const
|
||||
{
|
||||
float value = getValue();
|
||||
if (isnan(value)) return {};
|
||||
|
||||
PrometheusData res;
|
||||
|
||||
res.name = getMetricDotName() != "" ? getMetricDotName() : getMetricName();
|
||||
res.type = getMetricType() == MetricType::GAUGE ? "gauge" : "counter";
|
||||
res.desc = getMetircDescription();
|
||||
|
||||
stringstream labels;
|
||||
const auto &label_pairs = getBasicLabels();
|
||||
bool first = true;
|
||||
for (auto &pair : label_pairs) {
|
||||
if (!first) labels << ',';
|
||||
labels << pair.first << "=\"" << pair.second << '"';
|
||||
first = false;
|
||||
}
|
||||
res.label = labels.str();
|
||||
|
||||
stringstream value_str;
|
||||
value_str << value;
|
||||
res.value = value_str.str();
|
||||
|
||||
return {res};
|
||||
}
|
||||
|
||||
map<string, string>
|
||||
MetricCalc::getBasicLabels() const
|
||||
{
|
||||
map<string, string> res;
|
||||
|
||||
auto i_instance = Singleton::Consume<I_InstanceAwareness>::by<GenericMetric>();
|
||||
auto id = i_instance->getUniqueID();
|
||||
if (id.ok()) res["id"] = *id;
|
||||
|
||||
auto details = Singleton::Consume<I_AgentDetails>::by<GenericMetric>();
|
||||
res["agent"] = details->getAgentId();
|
||||
|
||||
auto env = Singleton::Consume<I_Environment>::by<GenericMetric>();
|
||||
auto executable = env->get<string>("Base Executable Name");
|
||||
if (executable.ok()) res["process"] = *executable;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static const string metric_file = "/tmp/metrics_output.txt";
|
||||
|
||||
class GenericMetric::MetricsRest : public ServerRest
|
||||
@@ -121,7 +188,9 @@ void
|
||||
GenericMetric::handleMetricStreamSending()
|
||||
{
|
||||
if (active_streams.isSet(Stream::DEBUG)) generateDebug();
|
||||
if (active_streams.isSet(Stream::PROMETHEUS)) generatePrometheus();
|
||||
if (active_streams.isSet(Stream::FOG)) generateLog();
|
||||
if (active_streams.isSet(Stream::AIOPS)) generateAiopsLog();
|
||||
|
||||
if (reset) resetMetrics();
|
||||
}
|
||||
@@ -244,6 +313,106 @@ GenericMetric::generateLog()
|
||||
sendLog(metric_client_rest);
|
||||
}
|
||||
|
||||
class PrometheusRest : public ClientRest
|
||||
{
|
||||
class Metric : public ClientRest
|
||||
{
|
||||
public:
|
||||
Metric(const string &n, const string &t, const string &d, const string &l, const string &v)
|
||||
:
|
||||
name(n),
|
||||
type(t),
|
||||
description(d),
|
||||
labels(l),
|
||||
value(v)
|
||||
{}
|
||||
|
||||
private:
|
||||
C2S_PARAM(string, name);
|
||||
C2S_PARAM(string, type);
|
||||
C2S_PARAM(string, description);
|
||||
C2S_PARAM(string, labels);
|
||||
C2S_PARAM(string, value);
|
||||
};
|
||||
|
||||
public:
|
||||
PrometheusRest() : metrics(vector<Metric>()) {}
|
||||
|
||||
void
|
||||
addMetric(const vector<PrometheusData> &vec)
|
||||
{
|
||||
auto &metric_vec = metrics.get();
|
||||
metric_vec.reserve(vec.size());
|
||||
for (auto &metric : vec) {
|
||||
metric_vec.emplace_back(metric.name, metric.type, metric.desc, "{" + metric.label + "}", metric.value);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
C2S_PARAM(vector<Metric>, metrics);
|
||||
};
|
||||
|
||||
void
|
||||
GenericMetric::generatePrometheus()
|
||||
{
|
||||
if (!getProfileAgentSettingWithDefault(false, "prometheus")) return;
|
||||
|
||||
vector<PrometheusData> all_metrics;
|
||||
for (auto &calc : calcs) {
|
||||
const auto &cal_metrics = calc->getPrometheusMetrics();
|
||||
all_metrics.insert(all_metrics.end(), cal_metrics.begin(), cal_metrics.end());
|
||||
}
|
||||
|
||||
PrometheusRest rest;
|
||||
rest.addMetric(all_metrics);
|
||||
|
||||
MessageMetadata new_config_req_md("127.0.0.1", 7465);
|
||||
new_config_req_md.setConnectioFlag(MessageConnectionConfig::ONE_TIME_CONN);
|
||||
new_config_req_md.setConnectioFlag(MessageConnectionConfig::UNSECURE_CONN);
|
||||
Singleton::Consume<I_Messaging>::by<GenericMetric>()->sendSyncMessage(
|
||||
HTTPMethod::POST,
|
||||
"/set-prometheus-data",
|
||||
rest,
|
||||
MessageCategory::GENERIC,
|
||||
new_config_req_md
|
||||
);
|
||||
}
|
||||
|
||||
void
|
||||
GenericMetric::generateAiopsLog()
|
||||
{
|
||||
if (!getConfigurationWithDefault<bool>(true, "metric", "aiopsMetricSendEnable")) return;
|
||||
|
||||
AiopsMetricList aiops_metrics;
|
||||
|
||||
for (auto &calc : calcs) {
|
||||
aiops_metrics.addMetrics(calc->getAiopsMetrics());
|
||||
}
|
||||
|
||||
set<ReportIS::Tags> tags;
|
||||
Report metric_to_fog(
|
||||
"AIOPS Metric Data",
|
||||
Singleton::Consume<I_TimeGet>::by<GenericMetric>()->getWalltime(),
|
||||
Type::PERIODIC,
|
||||
Level::LOG,
|
||||
LogLevel::INFO,
|
||||
audience,
|
||||
team,
|
||||
Severity::INFO,
|
||||
Priority::LOW,
|
||||
report_interval,
|
||||
LogField("agentId", Singleton::Consume<I_AgentDetails>::by<GenericMetric>()->getAgentId()),
|
||||
tags,
|
||||
Tags::INFORMATIONAL,
|
||||
issuing_engine
|
||||
);
|
||||
|
||||
metric_to_fog << LogField("eventObject", aiops_metrics);
|
||||
|
||||
LogRest metric_client_rest(metric_to_fog);
|
||||
sendLog(metric_client_rest);
|
||||
}
|
||||
|
||||
void
|
||||
GenericMetric::generateDebug()
|
||||
{
|
||||
@@ -270,6 +439,7 @@ GenericMetric::preload()
|
||||
{
|
||||
registerExpectedConfiguration<bool>("metric", "fogMetricSendEnable");
|
||||
registerExpectedConfiguration<bool>("metric", "debugMetricSendEnable");
|
||||
registerExpectedConfiguration<bool>("metric", "aiopsMetricSendEnable");
|
||||
registerExpectedConfiguration<bool>("metric", "fogMetricUri");
|
||||
registerExpectedConfiguration<string>("metric", "metricsOutputTmpFile");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user