MCPcopy Create free account
hub / github.com/Icinga/icinga2 / SerializeState

Method SerializeState

lib/icingadb/icingadb-objects.cpp:2798–2924  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2796}
2797
2798Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable)
2799{
2800 Dictionary::Ptr attrs = new Dictionary();
2801
2802 Host::Ptr host;
2803 Service::Ptr service;
2804
2805 tie(host, service) = GetHostService(checkable);
2806
2807 String id = GetObjectIdentifier(checkable);
2808
2809 /*
2810 * As there is a 1:1 relationship between host and host state, the host ID ('host_id')
2811 * is also used as the host state ID ('id'). These are duplicated to 1) avoid having
2812 * special handling for this in Icinga DB and 2) to have both a primary key and a foreign key
2813 * in the SQL database in the end. In the database 'host_id' ends up as foreign key 'host_state.host_id'
2814 * referring to 'host.id' while 'id' ends up as the primary key 'host_state.id'. This also applies for service.
2815 */
2816 attrs->Set("id", id);
2817 attrs->Set("environment_id", m_EnvironmentId);
2818 attrs->Set("state_type", Checkable::StateTypeToString(checkable->HasBeenChecked() ? checkable->GetStateType() : StateTypeHard).ToLower());
2819
2820 // TODO: last_hard/soft_state should be "previous".
2821 if (service) {
2822 attrs->Set("service_id", id);
2823 auto state = service->HasBeenChecked() ? service->GetState() : ServicePending;
2824 attrs->Set("soft_state", state);
2825 attrs->Set("hard_state", service->HasBeenChecked() ? service->GetLastHardState() : ServicePending);
2826 attrs->Set("severity", service->GetSeverity());
2827 attrs->Set("host_id", GetObjectIdentifier(host));
2828 } else {
2829 attrs->Set("host_id", id);
2830 auto state = host->HasBeenChecked() ? host->GetState() : HostPending;
2831 attrs->Set("soft_state", state);
2832 attrs->Set("hard_state", host->HasBeenChecked() ? host->GetLastHardState() : HostPending);
2833 attrs->Set("severity", host->GetSeverity());
2834 }
2835
2836 attrs->Set("previous_soft_state", GetPreviousState(checkable, service, StateTypeSoft));
2837 attrs->Set("previous_hard_state", GetPreviousState(checkable, service, StateTypeHard));
2838 attrs->Set("check_attempt", checkable->GetCheckAttempt());
2839
2840 attrs->Set("is_active", checkable->IsActive());
2841 attrs->Set("affects_children", checkable->AffectsChildren());
2842
2843 CheckResult::Ptr cr = checkable->GetLastCheckResult();
2844
2845 if (cr) {
2846 String rawOutput = cr->GetOutput();
2847 if (!rawOutput.IsEmpty()) {
2848 size_t lineBreak = rawOutput.Find("\n");
2849 String output = rawOutput.SubStr(0, lineBreak);
2850 if (!output.IsEmpty())
2851 attrs->Set("output", rawOutput.SubStr(0, lineBreak));
2852
2853 if (lineBreak > 0 && lineBreak != String::NPos) {
2854 String longOutput = rawOutput.SubStr(lineBreak+1, rawOutput.GetLength());
2855 if (!longOutput.IsEmpty())

Callers

nothing calls this directly

Calls 15

GetPreviousStateFunction · 0.85
ToLowerMethod · 0.80
HasBeenCheckedMethod · 0.80
GetLastHardStateMethod · 0.80
IsActiveMethod · 0.80
AffectsChildrenMethod · 0.80
FindMethod · 0.80
SubStrMethod · 0.80
GetCommandMethod · 0.80
CalculateLatencyMethod · 0.80
GetProblemMethod · 0.80

Tested by

no test coverage detected