2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <NGObjWeb/WODirectAction.h>
23 #include <NGObjWeb/WOApplication.h>
24 #include <NGObjWeb/WOResourceManager.h>
25 #include <NGObjWeb/WOContext.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 context] resourceLookupLanguages];
144 xslPath = [[app resourceManager] urlForResourceNamed:xslPath
147 request:[self request]];
148 if ([xslPath hasPrefix:@"/missingresource"])
151 response = [WOResponse responseWithRequest:[self request]];
152 [response setHeader:@"text/xml" forKey:@"content-type"];
154 stats = [[app statisticsStore] statistics];
156 [response appendContentString:@"<?xml version='1.0'?>\n"];
158 if ([xslPath length] > 0) {
159 [response appendContentString:@"<?xml-stylesheet type='text/xsl' href='"];
160 [response appendContentString:xslPath];
161 [response appendContentString:@"'?>"];
164 [response appendContentString:@"<application name='"];
165 [response appendContentString:[app name]];
166 [response appendContentString:@"'"];
167 [response appendContentString:
168 [NSString stringWithFormat:@" pid='%d'", getpid()]];
169 [response appendContentString:
170 @" xmlns:dt='urn:schemas-microsoft-com:datatypes'>\n"];
175 NSDictionary *pageStatistics;
177 /* application statistics */
179 e = [stats keyEnumerator];
180 while ((key = [e nextObject])) {
184 if ([key isEqualToString:@"pageStatistics"])
187 value = [stats objectForKey:key];
189 [response appendContentString:@" <"];
190 [response appendContentString:key];
192 if ((dt = [keyToDataType objectForKey:key])) {
193 [response appendContentString:@" dt:dt='"];
194 [response appendContentString:dt];
195 [response appendContentString:@"'"];
198 [response appendContentString:@">"];
200 [response appendContentHTMLString:[value stringValue]];
202 [response appendContentString:@"</"];
203 [response appendContentString:key];
204 [response appendContentString:@">\n"];
207 /* memory statistics */
212 mem = [app memoryStatistics];
214 if ([mem count] > 0) {
215 [response appendContentString:@" <memory>\n"];
217 e = [mem keyEnumerator];
218 while ((key = [e nextObject])) {
222 value = [mem objectForKey:key];
224 [response appendContentString:@" <"];
225 [response appendContentString:key];
227 if ((dt = [keyToDataType objectForKey:key])) {
228 [response appendContentString:@" dt:dt='"];
229 [response appendContentString:dt];
230 [response appendContentString:@"'"];
233 [response appendContentString:@">"];
235 [response appendContentHTMLString:[value stringValue]];
237 [response appendContentString:@"</"];
238 [response appendContentString:key];
239 [response appendContentString:@">\n"];
242 [response appendContentString:@" </memory>\n"];
246 /* page statistics */
248 pageStatistics = [stats objectForKey:@"pageStatistics"];
250 [response appendContentString:@" <pages>\n"];
252 e = [pageStatistics keyEnumerator];
253 while ((key = [e nextObject])) {
258 stats = [pageStatistics objectForKey:key];
259 e2 = [stats keyEnumerator];
261 [response appendContentString:@" <page name='"];
262 [response appendContentString:key];
263 [response appendContentString:@"'>\n"];
265 while ((key2 = [e2 nextObject])) {
269 value = [stats objectForKey:key2];
271 [response appendContentString:@" <"];
272 [response appendContentString:key2];
274 if ((dt = [keyToDataType objectForKey:key2])) {
275 [response appendContentString:@" dt:dt='"];
276 [response appendContentString:dt];
277 [response appendContentString:@"'"];
280 [response appendContentString:@">"];
282 [response appendContentHTMLString:[value stringValue]];
284 [response appendContentString:@"</"];
285 [response appendContentString:key2];
286 [response appendContentString:@">\n"];
288 [response appendContentString:@" </page>\n"];
291 [response appendContentString:@" </pages>\n"];
294 [response appendContentString:@"</application>\n"];
299 @end /* WODirectAction(WOStats) */