XEN subagent: added domain state parameters
[public/netxms.git] / src / agent / subagents / xen / xen.cpp
1 /*
2 ** NetXMS XEN hypervisor subagent
3 ** Copyright (C) 2017 Raden Solutions
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (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
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: xen.cpp
20 **
21 **/
22
23 #include "xen.h"
24
25 /**
26 * Handlers
27 */
28 LONG H_XenDomainCPUUsage(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
29 LONG H_XenDomainList(const TCHAR *param, const TCHAR *arg, StringList *value, AbstractCommSession *session);
30 LONG H_XenDomainNetIfTable(const TCHAR *param, const TCHAR *arg, Table *value, AbstractCommSession *session);
31 LONG H_XenDomainNetStats(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
32 LONG H_XenDomainState(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
33 LONG H_XenDomainTable(const TCHAR *param, const TCHAR *arg, Table *value, AbstractCommSession *session);
34 LONG H_XenHostCPUUsage(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
35 LONG H_XenHostOnlineCPUs(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
36 LONG H_XenHostPhyInfo(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
37 LONG H_XenHostVersion(const TCHAR *param, const TCHAR *arg, TCHAR *value, AbstractCommSession *session);
38
39 /**
40 * Convert XEN log level into NetXMS debug level
41 */
42 inline int ConvertLogLevel(xentoollog_level level)
43 {
44 if (level >= XTL_ERROR)
45 return 1;
46 if (level >= XTL_WARN)
47 return 3;
48 if (level >= XTL_NOTICE)
49 return 4;
50 return 5;
51 }
52
53 /**
54 * Write log message
55 */
56 static void LogMessage(struct xentoollog_logger *logger,
57 xentoollog_level level,
58 int errnoval /* or -1 */,
59 const char *context /* eg "xc", "xl", may be 0 */,
60 const char *format /* without level, context, \n */,
61 va_list al)
62 {
63 char msg[4096];
64 vsnprintf(msg, 4096, format, al);
65 nxlog_debug(ConvertLogLevel(level), _T("XEN: [%hs] (%hs): %hs"), xtl_level_to_string(level), context, msg);
66 }
67
68 /**
69 * Write progress message
70 */
71 static void LogProgress(struct xentoollog_logger *logger,
72 const char *context /* see above */,
73 const char *doing_what /* no \r,\n */,
74 int percent, unsigned long done, unsigned long total)
75 {
76 }
77
78 /**
79 * Destroy logger
80 */
81 static void LogDestroy(struct xentoollog_logger *logger)
82 {
83 }
84
85 /**
86 * Logger definition
87 */
88 xentoollog_logger g_xenLogger = { LogMessage, LogProgress, LogDestroy };
89
90 /**
91 * Called by master agent at unload
92 */
93 static void SubagentShutdown()
94 {
95 XenStopCPUCollector();
96 }
97
98 /**
99 * Subagent initialization
100 */
101 static BOOL SubagentInit(Config *config)
102 {
103 XenStartCPUCollector();
104 return TRUE;
105 }
106
107 /**
108 * Parameters
109 */
110 static NETXMS_SUBAGENT_PARAM s_parameters[] =
111 {
112 { _T("XEN.Domain.CPU.CurrentUsage(*)"), H_XenDomainCPUUsage, _T("0"), DCI_DT_FLOAT, _T("XEN domain {instance}: current CPU utilization") },
113 { _T("XEN.Domain.CPU.Usage(*)"), H_XenDomainCPUUsage, _T("1"), DCI_DT_FLOAT, _T("XEN domain {instance}: average CPU utilization for last minute") },
114 { _T("XEN.Domain.CPU.Usage15(*)"), H_XenDomainCPUUsage, _T("F"), DCI_DT_FLOAT, _T("XEN domain {instance}: average CPU utilization for last 15 minutes") },
115 { _T("XEN.Domain.CPU.Usage5(*)"), H_XenDomainCPUUsage, _T("5"), DCI_DT_FLOAT, _T("XEN domain {instance}: average CPU utilization for last 5 minutes") },
116 { _T("XEN.Domain.IsExist(*)"), H_XenDomainState, _T("E"), DCI_DT_INT, _T("XEN domain {instance}: existence flag") },
117 { _T("XEN.Domain.IsOperational(*)"), H_XenDomainState, _T("O"), DCI_DT_INT, _T("XEN domain {instance}: operational flag") },
118 { _T("XEN.Domain.IsPaused(*)"), H_XenDomainState, _T("P"), DCI_DT_INT, _T("XEN domain {instance}: paused flag") },
119 { _T("XEN.Domain.Net.RxBytes(*)"), H_XenDomainNetStats, _T("RB"), DCI_DT_UINT64, _T("XEN domain {instance}: network usage (bytes received)") },
120 { _T("XEN.Domain.Net.RxPackets(*)"), H_XenDomainNetStats, _T("RP"), DCI_DT_UINT64, _T("XEN domain {instance}: network usage (packets received)") },
121 { _T("XEN.Domain.Net.TxBytes(*)"), H_XenDomainNetStats, _T("TB"), DCI_DT_UINT64, _T("XEN domain {instance}: network usage (bytes transmitted)") },
122 { _T("XEN.Domain.Net.TxPackets(*)"), H_XenDomainNetStats, _T("TP"), DCI_DT_UINT64, _T("XEN domain {instance}: network usage (packets transmitted)") },
123 { _T("XEN.Domain.State(*)"), H_XenDomainState, _T("?"), DCI_DT_INT, _T("XEN domain {instance}: state") },
124 { _T("XEN.Host.CPU.Cores"), H_XenHostPhyInfo, _T("c"), DCI_DT_INT, _T("XEN host: number of CPU cores") },
125 { _T("XEN.Host.CPU.CurrentUsage"), H_XenHostCPUUsage, _T("0"), DCI_DT_FLOAT, _T("XEN host: current CPU utilization") },
126 { _T("XEN.Host.CPU.Frequency"), H_XenHostPhyInfo, _T("F"), DCI_DT_INT, _T("XEN host: number of CPU cores") },
127 { _T("XEN.Host.CPU.LogicalCount"), H_XenHostPhyInfo, _T("C"), DCI_DT_INT, _T("XEN host: number of logical CPUs") },
128 { _T("XEN.Host.CPU.Online"), H_XenHostOnlineCPUs, NULL, DCI_DT_INT, _T("XEN host: number of online CPUs") },
129 { _T("XEN.Host.CPU.PhysicalCount"), H_XenHostPhyInfo, _T("P"), DCI_DT_INT, _T("XEN host: number of physical CPUs") },
130 { _T("XEN.Host.CPU.Usage"), H_XenHostCPUUsage, _T("1"), DCI_DT_FLOAT, _T("XEN host: average CPU utilization for last minute") },
131 { _T("XEN.Host.CPU.Usage15"), H_XenHostCPUUsage, _T("F"), DCI_DT_FLOAT, _T("XEN host: average CPU utilization for last 15 minutes") },
132 { _T("XEN.Host.CPU.Usage5"), H_XenHostCPUUsage, _T("5"), DCI_DT_FLOAT, _T("XEN host: average CPU utilization for last 5 minutes") },
133 { _T("XEN.Host.Memory.Free"), H_XenHostPhyInfo, _T("M"), DCI_DT_UINT64, _T("XEN host: free memory") },
134 { _T("XEN.Host.Memory.FreePerc"), H_XenHostPhyInfo, _T("m"), DCI_DT_FLOAT, _T("XEN host: free memory (%)") },
135 { _T("XEN.Host.Memory.Outstanding"), H_XenHostPhyInfo, _T("O"), DCI_DT_UINT64, _T("XEN host: outstanding memory") },
136 { _T("XEN.Host.Memory.OutstandingPerc"), H_XenHostPhyInfo, _T("o"), DCI_DT_FLOAT, _T("XEN host: outstanding memory (%)") },
137 { _T("XEN.Host.Memory.Scrub"), H_XenHostPhyInfo, _T("S"), DCI_DT_UINT64, _T("XEN host: scrub memory") },
138 { _T("XEN.Host.Memory.ScrubPerc"), H_XenHostPhyInfo, _T("s"), DCI_DT_FLOAT, _T("XEN host: scrub memory (%)") },
139 { _T("XEN.Host.Memory.Total"), H_XenHostPhyInfo, _T("T"), DCI_DT_UINT64, _T("XEN host: total memory") },
140 { _T("XEN.Host.Memory.Used"), H_XenHostPhyInfo, _T("U"), DCI_DT_UINT64, _T("XEN host: used memory") },
141 { _T("XEN.Host.Memory.UsedPerc"), H_XenHostPhyInfo, _T("u"), DCI_DT_FLOAT, _T("XEN host: used memory (%)") },
142 { _T("XEN.Host.Version"), H_XenHostVersion, NULL, DCI_DT_STRING, _T("XEN host: version") }
143 };
144
145 /**
146 * Lists
147 */
148 static NETXMS_SUBAGENT_LIST s_lists[] =
149 {
150 { _T("XEN.Domains"), H_XenDomainList, NULL }
151 };
152
153 /**
154 * Tables
155 */
156 static NETXMS_SUBAGENT_TABLE s_tables[] =
157 {
158 { _T("XEN.Domains"), H_XenDomainTable, NULL, _T("ID"), _T("XEN: domains (virtual machines)") },
159 { _T("XEN.Net.DomainInterfaces"), H_XenDomainNetIfTable, NULL, _T("NAME"), _T("XEN: domain network interfaces") }
160 };
161
162 /**
163 * Subagent information
164 */
165 static NETXMS_SUBAGENT_INFO m_info =
166 {
167 NETXMS_SUBAGENT_INFO_MAGIC,
168 _T("XEN"), NETXMS_VERSION_STRING,
169 SubagentInit, SubagentShutdown, NULL,
170 sizeof(s_parameters) / sizeof(NETXMS_SUBAGENT_PARAM),
171 s_parameters,
172 sizeof(s_lists) / sizeof(NETXMS_SUBAGENT_LIST),
173 s_lists,
174 sizeof(s_tables) / sizeof(NETXMS_SUBAGENT_TABLE),
175 s_tables, // tables
176 0, NULL, // actions
177 0, NULL // push parameters
178 };
179
180 /**
181 * Entry point for NetXMS agent
182 */
183 DECLARE_SUBAGENT_ENTRY_POINT(XEN)
184 {
185 *ppInfo = &m_info;
186 return TRUE;
187 }
188
189 #ifdef _WIN32
190
191 /**
192 * DLL entry point
193 */
194 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
195 {
196 if (dwReason == DLL_PROCESS_ATTACH)
197 DisableThreadLibraryCalls(hInstance);
198 return TRUE;
199 }
200
201 #endif