2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <NGObjWeb/WODirectAction.h>
24 #include <NGObjWeb/WOApplication.h>
25 #include <NGObjWeb/WOResourceManager.h>
26 #include <NGObjWeb/WORequest.h>
27 #include <NGObjWeb/WOResponse.h>
28 #include <NGObjWeb/WOSession.h>
29 #include <NGObjWeb/WOStatisticsStore.h>
32 @interface WOApplication(MemoryStatistics)
34 - (NSDictionary *)memoryStatistics;
38 @implementation WOApplication(MemoryStatistics)
40 - (NSDictionary *)memoryStatistics {
47 sprintf(fname, "/proc/%d/status", getpid());
48 if ((f = fopen(fname, "r"))) {
53 len = fread(buf, 1, sizeof(buf) - 1, f);
57 s = [NSString stringWithCString:buf];
58 lines = [[s componentsSeparatedByString:@"\n"] objectEnumerator];
59 while ((s = [lines nextObject])) {
63 rng = [s rangeOfString:@":"];
67 key = [s substringToIndex:rng.location];
69 if ([key hasPrefix:@"Vm"]) {
74 while (*cstr != '\0' && *cstr != ':') cstr++;
75 if (*cstr == '\0') continue;
77 while (*cstr != '\0' && isspace(*cstr)) cstr++;
78 value = [NSString stringWithCString:cstr];
80 if ([value hasSuffix:@" kB"]) {
81 value = [value substringToIndex:[value length] - 3];
82 value = [NSNumber numberWithInt:[value intValue] * 1024];
85 if (d == nil) d = [NSMutableDictionary dictionary];
86 [d setObject:value forKey:key];
95 @end /* WOApplication(MemoryStatistics) */
97 @implementation WODirectAction(WOStats)
99 - (id<WOActionResults>)WOStatsAction {
101 WOResponse *response;
105 static NSDictionary *keyToDataType = nil;
107 if (keyToDataType == nil) {
109 [[NSDictionary alloc] initWithObjectsAndKeys:
110 @"number", @"averageDuration",
111 @"number", @"maximumDuration",
112 @"number", @"minimumDuration",
113 @"number", @"totalDuration",
114 @"number", @"instanceUptime",
115 @"number", @"instanceUptimeInHours",
116 @"number", @"instanceLoad",
117 @"number", @"pageResponseCount",
118 @"number", @"numberOfZippedResponses",
119 @"number", @"totalResponseCount",
120 @"number", @"pageFrequency",
121 @"number", @"pageDeliveryVolumne",
122 @"number", @"responseFrequency",
123 @"number", @"relativeTimeConsumption",
124 @"number", @"averageResponseSize",
125 @"number", @"totalResponseSize",
126 @"number", @"totalZippedSize",
127 @"number", @"smallestResponseSize",
128 @"number", @"largestResponseSize",
129 @"number", @"VmData",
134 @"number", @"VmSize",
139 app = [WOApplication application];
141 xslPath = [[NSUserDefaults standardUserDefaults]
142 stringForKey:@"WOStatsStylesheetName"];
143 languages = [self existingSession] != nil
144 ? [[self session] languages]
145 : [[self request] browserLanguages];
146 xslPath = [[app resourceManager] urlForResourceNamed:xslPath
149 request:[self request]];
150 if ([xslPath hasPrefix:@"/missingresource"])
153 response = [WOResponse responseWithRequest:[self request]];
154 [response setHeader:@"text/xml" forKey:@"content-type"];
156 stats = [[app statisticsStore] statistics];
158 [response appendContentString:@"<?xml version='1.0'?>\n"];
160 if ([xslPath length] > 0) {
161 [response appendContentString:@"<?xml-stylesheet type='text/xsl' href='"];
162 [response appendContentString:xslPath];
163 [response appendContentString:@"'?>"];
166 [response appendContentString:@"<application name='"];
167 [response appendContentString:[app name]];
168 [response appendContentString:@"'"];
169 [response appendContentString:
170 [NSString stringWithFormat:@" pid='%d'", getpid()]];
171 [response appendContentString:
172 @" xmlns:dt='urn:schemas-microsoft-com:datatypes'>\n"];
177 NSDictionary *pageStatistics;
179 /* application statistics */
181 e = [stats keyEnumerator];
182 while ((key = [e nextObject])) {
186 if ([key isEqualToString:@"pageStatistics"])
189 value = [stats objectForKey:key];
191 [response appendContentString:@" <"];
192 [response appendContentString:key];
194 if ((dt = [keyToDataType objectForKey:key])) {
195 [response appendContentString:@" dt:dt='"];
196 [response appendContentString:dt];
197 [response appendContentString:@"'"];
200 [response appendContentString:@">"];
202 [response appendContentHTMLString:[value stringValue]];
204 [response appendContentString:@"</"];
205 [response appendContentString:key];
206 [response appendContentString:@">\n"];
209 /* memory statistics */
214 mem = [app memoryStatistics];
216 if ([mem count] > 0) {
217 [response appendContentString:@" <memory>\n"];
219 e = [mem keyEnumerator];
220 while ((key = [e nextObject])) {
224 value = [mem objectForKey:key];
226 [response appendContentString:@" <"];
227 [response appendContentString:key];
229 if ((dt = [keyToDataType objectForKey:key])) {
230 [response appendContentString:@" dt:dt='"];
231 [response appendContentString:dt];
232 [response appendContentString:@"'"];
235 [response appendContentString:@">"];
237 [response appendContentHTMLString:[value stringValue]];
239 [response appendContentString:@"</"];
240 [response appendContentString:key];
241 [response appendContentString:@">\n"];
244 [response appendContentString:@" </memory>\n"];
248 /* page statistics */
250 pageStatistics = [stats objectForKey:@"pageStatistics"];
252 [response appendContentString:@" <pages>\n"];
254 e = [pageStatistics keyEnumerator];
255 while ((key = [e nextObject])) {
260 stats = [pageStatistics objectForKey:key];
261 e2 = [stats keyEnumerator];
263 [response appendContentString:@" <page name='"];
264 [response appendContentString:key];
265 [response appendContentString:@"'>\n"];
267 while ((key2 = [e2 nextObject])) {
271 value = [stats objectForKey:key2];
273 [response appendContentString:@" <"];
274 [response appendContentString:key2];
276 if ((dt = [keyToDataType objectForKey:key2])) {
277 [response appendContentString:@" dt:dt='"];
278 [response appendContentString:dt];
279 [response appendContentString:@"'"];
282 [response appendContentString:@">"];
284 [response appendContentHTMLString:[value stringValue]];
286 [response appendContentString:@"</"];
287 [response appendContentString:key2];
288 [response appendContentString:@">\n"];
290 [response appendContentString:@" </page>\n"];
293 [response appendContentString:@" </pages>\n"];
296 [response appendContentString:@"</application>\n"];
301 @end /* WODirectAction(WOStats) */