XRootD
Loading...
Searching...
No Matches
XrdMonitor.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d M o n i t o r . c c */
4/* */
5/* (c) 2024 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#include <stdio.h>
32#include <string.h>
33
34#include "Xrd/XrdMonitor.hh"
35#include "Xrd/XrdMonRoll.hh"
36#include "XrdSys/XrdSysError.hh"
37
38/******************************************************************************/
39/* G l o b a l O b j e c t s */
40/******************************************************************************/
41
42namespace XrdGlobal
43{
44extern XrdSysError Log;
45}
46using namespace XrdGlobal;
47
48/******************************************************************************/
49/* Private: F i n d S e t */
50/******************************************************************************/
51
52XrdMonitor::RegInfo* XrdMonitor::FindSet(const char* setName, int sType)
53{
54 for (auto it = regVec.begin(); it != regVec.end(); it++)
55 if ((*it)->setType & sType && !std::strcmp((*it)->setName, setName))
56 return *it;
57 return 0;
58}
59
60/******************************************************************************/
61/* F o r m a t */
62/******************************************************************************/
63
64int XrdMonitor::Format(char* buff, int bsize, int& item, int opts)
65{
66// Make sure we are in the range of things to format
67//
68 if (item < 0 || item >= (int)regVec.size()) return 0;
69
70// Skip over types that are not wanted
71//
72 while((regVec[item]->setType & opts) == 0)
73 {item++;
74 if (item >= (int)regVec.size()) return 0;
75 }
76
77// Format the item
78//
79 if (opts & F_JSON) bsize = FormJSON(*regVec[item], buff, bsize);
80 else bsize = FormXML (*regVec[item], buff, bsize);
81 item++;
82 return bsize;
83}
84
85/******************************************************************************/
86
87int XrdMonitor::Format(char* buff, int bsize, const char* setName, int opts)
88{
89// Find the set and format it
90//
91 RegInfo* regInfo = FindSet(setName, opts);
92 if (!regInfo) return 0;
93 if (opts & F_JSON) bsize = FormJSON(*regInfo, buff, bsize);
94 else bsize = FormXML (*regInfo, buff, bsize);
95 return bsize;
96}
97
98/******************************************************************************/
99/* Private: F o r m J S O N */
100/******************************************************************************/
101
102#define Updt(x) if (x >= bsize) return 0; bsize -= x; buff += x; bLen += x
103
104int XrdMonitor::FormJSON(XrdMonitor::RegInfo& regInfo, char* buff, int bsize)
105{
106 int n, bLen = 0;
107 unsigned int kVal;
108
109// Format the header
110//
111 n = snprintf(buff, bsize, "%s", regInfo.Json.hdr);
112 Updt(n);
113
114// Format all the variable
115//
116 for (int i = 0; i < (int)regInfo.Json.key.size(); i++)
117 {kVal = *regInfo.keyVal[i];
118 n = snprintf(buff, bsize, "%s%u", regInfo.Json.key[i], kVal);
119 Updt(n);
120 }
121
122// Insert end and return
123//
124 n = snprintf(buff, bsize, "}");
125 Updt(n);
126 return bLen;
127}
128
129/******************************************************************************/
130/* Private: F o r m X M L */
131/******************************************************************************/
132
133int XrdMonitor::FormXML(XrdMonitor::RegInfo& regInfo, char* buff, int bsize)
134{
135 int n, bLen = 0;
136 unsigned int kVal;
137
138// Format the header
139//
140 n = snprintf(buff, bsize, "%s", regInfo.Xml.hdr);
141 Updt(n);
142
143// Format all the variables
144//
145 for (int i = 0; i < (int)regInfo.Xml.keyBeg.size(); i++)
146 {kVal = *regInfo.keyVal[i];
147 n = snprintf(buff, bsize, "%s%u%s", regInfo.Xml.keyBeg[i], kVal,
148 regInfo.Xml.keyEnd[i]);
149 Updt(n);
150 }
151
152// Insert end and return
153//
154 n = snprintf(buff, bsize, "%s", "</stats>");
155 Updt(n);
156 return bLen;
157}
158
159/******************************************************************************/
160/* R e g i s t e r */
161/******************************************************************************/
162
163bool XrdMonitor::Register(XrdMonRoll::rollType setType, const char* setName,
164 XrdMonRoll::setMember setVec[])
165{ char buff[512];
166 int numE = 0;
167
168// Make sure this map has not been previously defined
169//
170 if (FindSet(setName,-1)) return false;
171
172// Count the number of elements
173//
174 for(int i = 0; setVec[i].varName; i++) numE++;
175 if (!numE) return true;
176
177// Determine type of set
178//
179 int sType;
180 switch(setType)
181 {case XrdMonRoll::AddOn: sType = isAdon; break;
182 case XrdMonRoll::Misc: sType = isAdon; break; // Deprecated
183 case XrdMonRoll::Plugin: sType = isPlug; break;
184 case XrdMonRoll::Protocol: sType = isPlug; break; // Deprecated
185 default: sType = isPlug; break;
186 }
187
188// Allocate a regInfo object
189//
190 RegInfo* regInfo = new RegInfo(setName, sType);
191 regInfo->Json.key.reserve(numE);
192 regInfo->Xml.keyBeg.reserve(numE);
193 regInfo->Xml.keyEnd.reserve(numE);
194 regInfo->keyVal = new RAtomic_uint*[numE]();
195
196// Create the registry
197//
198 for(int i = 0;i < numE; i++)
199 {snprintf(buff, sizeof(buff), ",\"%s\":", setVec[i].varName);
200 regInfo->Json.key.push_back(strdup(buff));
201 snprintf(buff, sizeof(buff), "<%s>", setVec[i].varName);
202 regInfo->Xml.keyBeg.push_back(strdup(buff));
203 snprintf(buff, sizeof(buff), "</%s>", setVec[i].varName);
204 regInfo->Xml.keyEnd.push_back(strdup(buff));
205 regInfo->keyVal[i] = &setVec[i].varValu;
206 }
207 regInfo->Json.key[0]++;
208
209// Complete the registry
210//
211 snprintf(buff, sizeof(buff), "\"stats_%s\":{", setName);
212 regInfo->Json.hdr = strdup(buff);
213 snprintf(buff, sizeof(buff), "<stats id=\"%s\">", setName);
214 regInfo->Xml.hdr = strdup(buff);
215
216// Insert registry into the map
217//
218 regVec.push_back(regInfo);
219
220// Display information
221//
222 char etxt[256];
223 snprintf(etxt, sizeof(etxt), "%s set %s registered with %d variable(s)",
224 (setType == XrdMonRoll::Misc ? "plugin" : "protocol"),setName,numE);
225 Log.Say("Config monitor: ", etxt);
226 return true;
227}
#define Updt(x)
struct myOpts opts
XrdSys::RAtomic< unsigned int > RAtomic_uint
const char * varName
Definition XrdMonRoll.hh:60
RAtomic_uint & varValu
Definition XrdMonRoll.hh:60
int Format(char *buff, int bsize, int &item, int opts=0)
Definition XrdMonitor.cc:64
bool Register(XrdMonRoll::rollType setType, const char *setName, XrdMonRoll::setMember setVec[])
XrdSysError Log
Definition XrdConfig.cc:113