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