1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
32 using namespace icinga;
35 REGISTER_DBTYPE(Endpoint, "endpoint", DbObjectTypeEndpoint, "endpoint_object_id", EndpointDbObject);
37 INITIALIZE_ONCE(&EndpointDbObject::StaticInitialize);
39 void EndpointDbObject::StaticInitialize(void)
41 Endpoint::OnConnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
42 Endpoint::OnDisconnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
45 EndpointDbObject::EndpointDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
46 : DbObject(type, name1, name2)
49 Dictionary::Ptr EndpointDbObject::GetConfigFields(void) const
51 Dictionary::Ptr fields = make_shared<Dictionary>();
52 Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
54 fields->Set("identity", endpoint->GetName());
55 fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
60 Dictionary::Ptr EndpointDbObject::GetStatusFields(void) const
62 Dictionary::Ptr fields = make_shared<Dictionary>();
63 Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
65 Log(LogDebug, "EndpointDbObject")
66 << "update status for endpoint '" << endpoint->GetName() << "'";
68 fields->Set("identity", endpoint->GetName());
69 fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
70 fields->Set("is_connected", EndpointIsConnected(endpoint));
75 void EndpointDbObject::UpdateConnectedStatus(const Endpoint::Ptr& endpoint)
77 bool connected = EndpointIsConnected(endpoint);
79 Log(LogDebug, "EndpointDbObject")
80 << "update is_connected=" << connected << " for endpoint '" << endpoint->GetName() << "'";
83 query1.Table = "endpointstatus";
84 query1.Type = DbQueryUpdate;
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;
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 */
98 int EndpointDbObject::EndpointIsConnected(const Endpoint::Ptr& endpoint)
100 unsigned int is_connected = endpoint->IsConnected() ? 1 : 0;
102 /* if identity is equal to node, fake is_connected */
103 if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
109 void EndpointDbObject::OnConfigUpdate(void)
111 /* update current status on config dump once */
112 Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
115 query1.Table = "endpointstatus";
116 query1.Type = DbQueryInsert;
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;