1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
<HTML><HEAD>
<TITLE>C Examples -- Example 4: stats</TITLE>
<LINK rel=Previous href="c-app3.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="c-app5.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="c-app3.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-app5.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="16333">
</a><h3>Example 4: stats</h3>
<p><a name="16348">
</a>The following example provides several cooperating facilities to implement a simple connection statistics system. The module includes a trace to collect the statistics on each connection, a scheduled procedure to aggregate the statistics, the /NS/Stat request function to print the statistics, and a Tcl command to access the statistics in a script.</p>
<p><a name="16335">
</a>This example can be found in the <code>examples/c/stats</code> directory.</p>
<pre> <a name="15846"></a>
<a name="16371"></a>/*
<a name="16376"></a> * stat.c - Example C module.
<a name="16377"></a> *
<a name="16378"></a> * This example incorporates many of the features of the AOLserver
<a name="16379"></a> * C API in a single simple, but useful, statistics gathering module.
<a name="16380"></a> * The module maitains statistics on the number of connections and
<a name="16381"></a> * bytes sent by a server. The data is stored in a simple
<a name="16382"></a> * `StatContext' structure which is allocated when the module is
<a name="16383"></a> * loaded. The StatContext stores the current, last, and total numbers
<a name="16384"></a> * and is updated after each connection by the `StatTrace' connection
<a name="16385"></a> * trace procedure. At a regular interval (the interval in seconds
<a name="16386"></a> * is configurable), the `StatUpdate' funcation is called to log the
<a name="16387"></a> * current and total number to the AOLserver log file and then
<a name="16388"></a> * makes the current data the last data. Access to these data is
<a name="16389"></a> * provided in two forms:
<a name="16390"></a> *
<a name="16391"></a> * 1. A simple C request function, `StatRequest', will return the
<a name="16392"></a> * current data as a simple notice page. The StatRequest function
<a name="16393"></a> * is registered at the GET /NS/Stat URL of the server.
<a name="16394"></a> *
<a name="16395"></a> * 2. The `ns_stat' Tcl command is added to each Tcl interpreter
<a name="16396"></a> * of the server interpreter pool. This allows a Tcl script
<a name="16397"></a> * to access the current, last, or total data at any time.
<a name="16398"></a> *
<a name="16399"></a> * Because the AOLserver is completely multithreaded, the statistics
<a name="16400"></a> * data can be updated and queried simultaneously. An Ns_Mutex is
<a name="16401"></a> * included in the ServerContext structure to keep multiple threads
<a name="16402"></a> * from accessing the data at the same time.
<a name="16403"></a> *
<a name="16404"></a> * Finally, on shutdown, the `StatShutdown' function is called to
<a name="16405"></a> * print out a final tally and clean up the ServerContext structure.
<a name="16406"></a> *
<a name="16407"></a> * Note that all the features of this module are specific to the
<a name="16408"></a> * server it is loaded into. The server handle, `hServer',
<a name="16409"></a> * is used to makes sure the trace, request, Tcl command, and
<a name="16410"></a> * shutdown functions are all run in the context of the single
<a name="16411"></a> * server. Other virtaul servers may ignore or load the
<a name="16412"></a> * stat module as well and the AOLserver guarantees the data
<a name="16413"></a> * will be keep separate for each server.
<a name="16417"></a>*/
<a name="16418"></a>
<a name="16419"></a>
<a name="16420"></a>
<a name="16421"></a>#include "ns.h"
<a name="16422"></a>#include "nstcl.h"
<a name="16423"></a>
<a name="16424"></a>/*
<a name="16425"></a> * The default statistics update interval is 1 minute or 60 seconds.
<a name="16426"></a> */
<a name="16427"></a>#define DEFAULT_INTERVAL 60
<a name="16428"></a>
<a name="16429"></a>/*
<a name="16430"></a> * Definitions of the StatContext structure which is allocated
<a name="16431"></a> * on a per-server basis.
<a name="16432"></a> */
<a name="16433"></a>typedef struct {
<a name="16434"></a> int bytes;
<a name="16435"></a> int conns;
<a name="16436"></a>} StatData;
<a name="16437"></a>
<a name="16438"></a>typedef struct {
<a name="16439"></a> Ns_Mutex lock;
<a name="16440"></a> int interval;
<a name="16441"></a> char *hServer;
<a name="16442"></a> StatData current;
<a name="16443"></a> StatData last;
<a name="16444"></a> StatData total;
<a name="16445"></a>} StatContext;
<a name="16446"></a>
<a name="16447"></a>
<a name="16448"></a>/*
<a name="16449"></a> * Forward declarations of functions which will be referenced
<a name="16450"></a> * in the stat module initialization function.
<a name="16451"></a> */
<a name="16452"></a>static Tcl_CmdProc StatCmd;
<a name="16453"></a>static Ns_TclInterpInitProc StatTclInit;
<a name="16454"></a>static Ns_TraceProc StatTrace;
<a name="16455"></a>static Ns_Callback StatUpdate;
<a name="16456"></a>static Ns_Callback StatShutdown;
<a name="16457"></a>static Ns_OpProc StatRequest;
<a name="16458"></a>
<a name="16459"></a>/*
<a name="16460"></a> * The Ns_ModuleVersion exported integer is used to verify
<a name="16461"></a> * this module version when loaded. For AOLserver 2.0,
<a name="16462"></a> * 1 (one) is the only valid value for this variable.
<a name="16463"></a> */
<a name="16464"></a>int Ns_ModuleVersion = 1;
<a name="16465"></a>
<a name="16466"></a>/*
<a name="16467"></a> * The Ns_ModuleInit function is the function the AOLserver
<a name="16468"></a> * will call each time the module is loaded into a
<a name="16469"></a> * server. The function is passed two parameters:
<a name="16470"></a> *
<a name="16471"></a> * hServer: The server `handle' as a string. This is the
<a name="16472"></a> * short name given to the virutal server such
<a name="16473"></a> * as `server1'.
<a name="16474"></a> *
<a name="16475"></a> * hModule: The module `handle' as a string. This is the
<a name="16476"></a> * short name given to the module such as `stat'
<a name="16477"></a> *
<a name="16478"></a> * For example, if this module is known as `stat' and loaded
<a name="16479"></a> * into the `server1' server with entries similar to the following
<a name="16480"></a> * in the nsd.ini file:
<a name="16481"></a> *
<a name="16482"></a> * [ns\servers]
<a name="16483"></a> * server1=My First Server
<a name="16484"></a> *
<a name="16485"></a> * [ns\server1\modules]
<a name="16486"></a> * stat=stat.dll
<a name="16487"></a> *
<a name="16488"></a> * This function would be called with "server1" and "stat" as
<a name="16489"></a> * its arguments.
<a name="16490"></a> *
<a name="16491"></a> */
<a name="16492"></a>int
<a name="16493"></a>Ns_ModuleInit(char *hServer, char *hModule)
<a name="16494"></a>{
<a name="16495"></a> char *configPath;
<a name="16496"></a> StatContext *ctx;
<a name="16497"></a>
<a name="16498"></a>
<a name="16499"></a> /*
<a name="16500"></a> * Create and initalize the statistics context.
<a name="16501"></a> */
<a name="16502"></a> ctx = ns_malloc(sizeof(StatContext));
<a name="16503"></a> Ns_InitializeMutex(&ctx->lock);
<a name="16504"></a> ctx->hServer = hServer;
<a name="16505"></a> ctx->current.bytes = 0;
<a name="16506"></a> ctx->current.conns = 0;
<a name="16507"></a> ctx->last.bytes = 0;
<a name="16508"></a> ctx->last.conns = 0;
<a name="16509"></a> ctx->total.bytes = 0;
<a name="16510"></a> ctx->total.conns = 0;
<a name="16511"></a>
<a name="16512"></a> /*
<a name="16513"></a> * Determine the statistics interval from the config file.
<a name="16514"></a> * The Ns_ConfigGetPath function will expand to the
<a name="16515"></a> * `server module specific' configuration section, e.g.,
<a name="16516"></a> * [ns\server1\module\stat].
<a name="16517"></a> */
<a name="16518"></a> configPath = Ns_ConfigGetPath(hServer, hModule, NULL);
<a name="16519"></a> if (!Ns_ConfigGetInt(configPath, "Interval", &ctx->interval)) {
<a name="16520"></a> ctx->interval = DEFAULT_INTERVAL;
<a name="16521"></a> }
<a name="16522"></a>
<a name="16523"></a> /*
<a name="16524"></a> * Register the trace to accumulate the statistics.
<a name="16525"></a> */
<a name="16526"></a> Ns_RegisterServerTrace(hServer, StatTrace, ctx);
<a name="16527"></a>
<a name="16528"></a> /*
<a name="16529"></a> * Register the statistics update function to run at
<a name="16530"></a> * regular intervals.
<a name="16531"></a> */
<a name="16532"></a> Ns_ScheduleProc(StatUpdate, ctx, 0, ctx->interval);
<a name="16533"></a>
<a name="16534"></a> /*
<a name="16535"></a> * Add the GET /NS/Stat request function.
<a name="16536"></a> */
<a name="16537"></a> Ns_RegisterRequest(hServer, "GET", "/NS/Stat", StatRequest,
<a name="16758"></a> NULL, ctx, 0);
<a name="16538"></a>
<a name="16539"></a> /*
<a name="16540"></a> * Add the Tcl "ns_stat" command to the interpreter pool.
<a name="16541"></a> * Note how the context is passed to StatTclInit which
<a name="16542"></a> * then passes it to the Tcl_CreateCommand function.
<a name="16543"></a> */
<a name="16544"></a> Ns_TclInitInterps(hServer, StatTclInit, ctx);
<a name="16545"></a>
<a name="16546"></a> /*
<a name="16547"></a> * Register the statistics shutdown procedure which
<a name="16548"></a> * cleans up the context on server shutdown.
<a name="16549"></a> */
<a name="16550"></a> Ns_RegisterServerShutdown(hServer, StatShutdown, ctx);
<a name="16551"></a>
<a name="16552"></a> return NS_OK;
<a name="16553"></a>}
<a name="16554"></a>
<a name="16555"></a>
<a name="16556"></a>
<a name="17884"></a>
<a name="17885"></a>
<a name="17886"></a>
<a name="18826"></a>
<a name="18827"></a>
<a name="17887"></a>
<a name="17888"></a>/*
<a name="16557"></a> * StatTrace is called after each connection and accumulates the
<a name="16558"></a> * statistics.
<a name="16559"></a> */
<a name="16560"></a>static void
<a name="16561"></a>StatTrace(void *ctx, Ns_Conn *conn)
<a name="16562"></a>{
<a name="16563"></a> StatContext *sc;
<a name="16564"></a> int bytes;
<a name="16565"></a>
<a name="16566"></a> sc = (StatContext *) ctx;
<a name="16567"></a> Ns_LockMutex(&sc->lock);
<a name="16568"></a> bytes = Ns_ConnResponseLength(conn);
<a name="16569"></a> sc->current.bytes += bytes;
<a name="16570"></a> sc->total.bytes += bytes;
<a name="16571"></a> ++sc->current.conns;
<a name="16572"></a> ++sc->total.conns;
<a name="16573"></a> Ns_UnlockMutex(&sc->lock);
<a name="16574"></a>}
<a name="16575"></a>
<a name="16576"></a>
<a name="16577"></a>/*
<a name="16578"></a> * StatUpdate aggregates the statistics and logs the data to
<a name="16579"></a> * the AOLserver server log.
<a name="16580"></a> */
<a name="16581"></a>static void
<a name="16582"></a>StatUpdate(void *ctx)
<a name="16583"></a>{
<a name="16584"></a> StatContext *sc;
<a name="16585"></a>
<a name="16586"></a> sc = (StatContext *) ctx;
<a name="16587"></a>
<a name="16588"></a> Ns_LockMutex(&sc->lock);
<a name="16589"></a> Ns_Log(Notice,
<a name="16759"></a> "StatUpdate(%s): Last: conns %d, bytes %d Total: conns %d, bytes %d",
<a name="16590"></a> sc->hServer, sc->current.conns, sc->current.bytes,
<a name="16591"></a> sc->total.conns, sc->total.bytes);
<a name="16592"></a> sc->last.conns = sc->current.conns;
<a name="16593"></a> sc->last.bytes = sc->current.bytes;
<a name="16594"></a> sc->current.conns = 0;
<a name="16595"></a> sc->current.bytes = 0;
<a name="16596"></a> Ns_UnlockMutex(&sc->lock);
<a name="16597"></a>}
<a name="16598"></a>
<a name="16599"></a>
<a name="16600"></a>
<a name="17889"></a>
<a name="17890"></a>
<a name="17891"></a>
<a name="17892"></a>/*
<a name="16601"></a> * StatTclInit is called once for each Tcl interpreter
<a name="16602"></a> * in the virutal server's Tcl interpreter pool.
<a name="16603"></a> */
<a name="16604"></a>static int
<a name="16605"></a>StatTclInit(Tcl_Interp *interp, void *ctx)
<a name="16606"></a>{
<a name="16607"></a> Tcl_CreateCommand(interp, "ns_stat", StatCmd, ctx, NULL);
<a name="16608"></a> return NS_OK;
<a name="16609"></a>}
<a name="16610"></a>
<a name="16611"></a>
<a name="16612"></a>/*
<a name="16613"></a> * StatCmd implements the Tcl "ns_stat" command.
<a name="16614"></a> */
<a name="16615"></a>static int
<a name="16616"></a>StatCmd(ClientData ctx, Tcl_Interp *interp, int argc, char **argv)
<a name="16617"></a>{
<a name="16618"></a> StatContext *sc;
<a name="16619"></a> StatData *sd;
<a name="16620"></a>
<a name="16621"></a> sc = (StatContext *) ctx;
<a name="16622"></a> if (argc != 2) {
<a name="16623"></a> Tcl_AppendResult(interp, "wrong # args: should be \"",
<a name="16624"></a> argv[0], " command\"", NULL);
<a name="16625"></a> return TCL_ERROR;
<a name="16626"></a> }
<a name="16627"></a> if (strcmp(argv[1], "current") == 0) {
<a name="16628"></a> sd = &sc->current;
<a name="16629"></a> } else if (strcmp(argv[1], "last") == 0) {
<a name="16630"></a> sd = &sc->last;
<a name="16631"></a> } else if (strcmp(argv[1], "total") == 0) {
<a name="16632"></a> sd = &sc->total;
<a name="16633"></a> } else {
<a name="16634"></a> Tcl_AppendResult(interp, "unknown command \"",
<a name="16635"></a> argv[1], "\": should be current, last, or total", NULL);
<a name="16636"></a> return TCL_ERROR;
<a name="16637"></a> }
<a name="16638"></a>
<a name="16639"></a> Ns_LockMutex(&sc->lock);
<a name="16640"></a> sprintf(interp->result, "%d %d", sd->conns, sd->bytes);
<a name="16641"></a> Ns_UnlockMutex(&sc->lock);
<a name="16642"></a> return TCL_OK;
<a name="16643"></a>}
<a name="16644"></a>
<a name="16645"></a>
<a name="16646"></a>
<a name="17893"></a>
<a name="17894"></a>
<a name="17895"></a>/*
<a name="16647"></a> * StatRequest is a simple AOLserver request function which returns
<a name="16648"></a> * the current data in a simple HTML page. The page is generated
<a name="16649"></a> * with the Ns_ConnReturnNotice() function which is used throughout the
<a name="16650"></a> * AOLserver to generate simple HTML page responses with the
<a name="16651"></a> * AOLserver banner logo. Ns_ConnReturnNotice() takes a string as
<a name="16652"></a> * the HTML page content. We use an Ns_DString to quickly build
<a name="16653"></a> * up the string - Ns_DString's grow as needed so we don't have
<a name="16654"></a> * to worry about buffer overflow. The HTML page is a simple
<a name="16655"></a> * HTML3 <TABLE> which formats the current, last, and total
<a name="16656"></a> * statistics for the server.
<a name="16657"></a> */
<a name="16658"></a>static int
<a name="16659"></a>StatRequest(void *ctx, Ns_Conn *conn)
<a name="16660"></a>{
<a name="16661"></a> StatContext *sc;
<a name="16662"></a> Ns_DString ds;
<a name="16663"></a> int retcode;
<a name="16664"></a>
<a name="16665"></a> sc = (StatContext *) ctx;
<a name="16666"></a> Ns_DStringInit(&ds);
<a name="16668"></a> /*
<a name="16669"></a> * Build up the HTML3 <TABLE> with the latest data.
<a name="16670"></a> */
<a name="16671"></a> Ns_DStringAppend(&ds, "<table border cellpadding=\"10\">");
<a name="16672"></a> Ns_DStringVarAppend(&ds, "<tr><th>", sc->hServer, "</th>", NULL);
<a name="16673"></a> Ns_DStringAppend(&ds,
<a name="16760"></a> "<th>Current</th><th>Last</th><th>Total</th></tr>");
<a name="16674"></a> Ns_LockMutex(&sc->lock);
<a name="16675"></a> Ns_DStringPrintf(&ds,
<a name="16761"></a> "<tr><th># connections</th><td>%d</td><td>%d</td><td>%d</td></tr>",
<a name="16676"></a> sc->current.conns, sc->last.conns, sc->total.conns);
<a name="16677"></a> Ns_DStringPrintf(&ds,
<a name="16762"></a> "<tr><th># bytes</th><td>%d</td><td>%d</td><td>%d</td></tr>",
<a name="16678"></a> sc->current.bytes, sc->last.bytes, sc->total.bytes);
<a name="16679"></a> Ns_UnlockMutex(&sc->lock);
<a name="16680"></a> Ns_DStringAppend(&ds, "</table>");
<a name="16682"></a> /*
<a name="16683"></a> * Return the HTML page using Ns_ConnReturnNotice().
<a name="16684"></a> */
<a name="16685"></a> retcode = Ns_ConnReturnNotice(conn, 200, "Server Statistics",
ds.string);
<a name="16687"></a> /*
<a name="16688"></a> * Don't forget to free the dstring!
<a name="16689"></a> */
<a name="16690"></a> Ns_DStringFree(&ds);
<a name="16691"></a>
<a name="16692"></a> return retcode;
<a name="16693"></a>}
<a name="16694"></a>
<a name="16695"></a>
<a name="16696"></a>/*
<a name="16697"></a> * StatShutdown simple prints a final statistics entry to the
<a name="16698"></a> * server log and then cleans up the StatContext structure.
<a name="16699"></a> */
<a name="16700"></a>static void
<a name="16701"></a>StatShutdown(void *ctx)
<a name="16702"></a>{
<a name="16703"></a> StatContext *sc;
<a name="16704"></a>
<a name="16705"></a> sc = (StatContext *) ctx;
<a name="16706"></a> Ns_Log(Notice, "StatShutdown(%s): Total: conns %d, bytes %d.",
<a name="16707"></a> sc->hServer, sc->total.conns, sc->total.bytes);
<a name="16708"></a> Ns_DestroyMutex(&sc->lock);
<a name="16709"></a> ns_free(ctx);
<a name="16710"></a>}
<a name="16711"></a>
</pre><p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="c-app3.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-app5.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|