]> granicus.if.org Git - icinga2/blob - lib/db_ido/endpointdbobject.cpp
Fix Zone::IsGlobal()
[icinga2] / lib / db_ido / endpointdbobject.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "db_ido/endpointdbobject.hpp"
21 #include "db_ido/dbtype.hpp"
22 #include "db_ido/dbvalue.hpp"
23 #include "icinga/icingaapplication.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/initialize.hpp"
26 #include "base/dynamictype.hpp"
27 #include "base/utility.hpp"
28 #include "base/convert.hpp"
29 #include "base/logger.hpp"
30 #include <boost/foreach.hpp>
31
32 using namespace icinga;
33
34
35 REGISTER_DBTYPE(Endpoint, "endpoint", DbObjectTypeEndpoint, "endpoint_object_id", EndpointDbObject);
36
37 INITIALIZE_ONCE(&EndpointDbObject::StaticInitialize);
38
39 void EndpointDbObject::StaticInitialize(void)
40 {
41         Endpoint::OnConnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
42         Endpoint::OnDisconnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
43 }
44
45 EndpointDbObject::EndpointDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
46         : DbObject(type, name1, name2)
47 { }
48
49 Dictionary::Ptr EndpointDbObject::GetConfigFields(void) const
50 {
51         Dictionary::Ptr fields = make_shared<Dictionary>();
52         Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
53
54         fields->Set("identity", endpoint->GetName());
55         fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
56
57         return fields;
58 }
59
60 Dictionary::Ptr EndpointDbObject::GetStatusFields(void) const
61 {
62         Dictionary::Ptr fields = make_shared<Dictionary>();
63         Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
64
65         Log(LogDebug, "EndpointDbObject")
66             << "update status for endpoint '" << endpoint->GetName() << "'";
67
68         fields->Set("identity", endpoint->GetName());
69         fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
70         fields->Set("is_connected", EndpointIsConnected(endpoint));
71
72         return fields;
73 }
74
75 void EndpointDbObject::UpdateConnectedStatus(const Endpoint::Ptr& endpoint)
76 {
77         bool connected = EndpointIsConnected(endpoint);
78
79         Log(LogDebug, "EndpointDbObject")
80             << "update is_connected=" << connected << " for endpoint '" << endpoint->GetName() << "'";
81
82         DbQuery query1;
83         query1.Table = "endpointstatus";
84         query1.Type = DbQueryUpdate;
85
86         Dictionary::Ptr fields1 = make_shared<Dictionary>();
87         fields1->Set("is_connected", (connected ? 1 : 0));
88         fields1->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
89         query1.Fields = fields1;
90
91         query1.WhereCriteria = make_shared<Dictionary>();
92         query1.WhereCriteria->Set("endpoint_object_id", endpoint);
93         query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
94
95         OnQuery(query1);
96 }
97
98 int EndpointDbObject::EndpointIsConnected(const Endpoint::Ptr& endpoint)
99 {
100         unsigned int is_connected = endpoint->IsConnected() ? 1 : 0;
101
102         /* if identity is equal to node, fake is_connected */
103         if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
104                 is_connected = 1;
105
106         return is_connected;
107 }
108
109 void EndpointDbObject::OnConfigUpdate(void)
110 {
111         /* update current status on config dump once */
112         Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
113
114         DbQuery query1;
115         query1.Table = "endpointstatus";
116         query1.Type = DbQueryInsert;
117
118         Dictionary::Ptr fields1 = make_shared<Dictionary>();
119         fields1->Set("identity", endpoint->GetName());
120         fields1->Set("node", IcingaApplication::GetInstance()->GetNodeName());
121         fields1->Set("is_connected", EndpointIsConnected(endpoint));
122         fields1->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
123         fields1->Set("endpoint_object_id", endpoint);
124         fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
125         query1.Fields = fields1;
126
127         OnQuery(query1);
128 }