aboutsummaryrefslogtreecommitdiff
path: root/cvs/cvsweb.cgi/~checkout~/code/nelns/admin_executor_service/admin_executor_service.cpp?rev=1.1&content-type=text/plain/index.html
blob: 8a183cb4b23c178dfc08b8596616db71736a0e53 (plain)
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
/** \file admin_executor_service.cpp
 * Admin Executor Service (AES)
 *
 * $Id: admin_executor_service.cpp,v 1.1 2001/04/18 13:54:25 valignat Exp $
 */

/* Copyright, 2000 Nevrax Ltd.
 *
 * This file is part of NEVRAX NeL Network Services
 * NEVRAX NeL Network Services is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * NEVRAX NeL Network Services is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NEVRAX NeL Network Services; see the file COPYING. If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include <nel/misc/debug.h>
#include <nel/misc/log.h>
#include <nel/misc/common.h>
#include <nel/net/service.h>
#include <nel/net/net_displayer.h>

#include "sysload.h"
#include "sysmem.h"
#include "sysswap.h"

using namespace std;
using namespace NLMISC;
using namespace NLNET;


static const char UNKNOW_VALUE[] = "<Unknown>";


CLog StatLog(CLog::LOG_STAT);


/// Log Stat data (STT)
void sendData (const char *str, TSenderId from)
{
	StatLog.displayNL( str );
}


/// Log the last minute average load
void cbLoad ( CMessage& message, TSenderId from )
{
	CSysLoad sysload = CSysLoad();
	double   load    = sysload.getLoadInfo();
	string   answer( "LOAD " );

	if ( load < 0 )
	{
		answer.append( UNKNOW_VALUE );
	}
	else
	{
		char str[6];
		smprintf( str, 6, "%.2f", load );
		answer.append( string(str) );
	}
	
	sendData( answer.c_str(), from );
}


/// Log the total memory size and the used memory size (in B)
void cbMemory ( CMessage& message, TSenderId from )
{
	CSysMemory sysmem   = CSysMemory();
	sint       memUsage = sysmem.getMemoryUsage();
	string     answer( "MEM " );

	if ( memUsage < 0 )
	{
		answer.append( UNKNOW_VALUE );
	}
	else
	{
		char str[3];
		smprintf( str, 3, "%d", memUsage );
		answer.append( string(str) );
	}
	
	sendData( answer.c_str(), from );
}


/// Log the swap size and the used swap space (in kB)
void cbSwap ( CMessage& message, TSenderId from )
{
	CSysSwap sysswap   = CSysSwap();
	sint     swapUsage = sysswap.getSwapUsage();
	string   answer( "SWAP " );

	if ( swapUsage < 0 )
	{
		answer.append( UNKNOW_VALUE );
	}
	else
	{
		char str[3];
		smprintf( str, 3, "%d", swapUsage );
		answer.append( string(str) );
	}
	
	sendData( answer.c_str(), from );
}


// Log all the server informations.
void cbSystem ( CMessage& message, TSenderId from )
{
	cbLoad   ( message, from );
	cbMemory ( message, from );
	cbSwap   ( message, from );
}


/**
 * Callback Array
 * Message types:
 *	LOAD:   log Load information
 *	MEMORY: log Memory information
 *	SWAP:   log Swap information
 *	SYSTEM: log the Load, Memory, and Swap informations
 */
TCallbackItem CallbackArray[] =
{
	{ "LOAD",   cbLoad },
	{ "MEMORY", cbMemory },
	{ "SWAP",   cbSwap },

	{ "SYSTEM", cbSystem }
};



/** Admin Executor Service (AES).
 * Log informations (load, memory usage, etc ...) about the server it's
 * running on.
 * These informations are used by the Admin Service (AS) and the Naming
 * Service (NS) to watch the differents servers of the shard.
 */
class CAdminExecutorService : public NLNET::IService
{
public:

	/// Initializes the service
	void init ()
	{
		// Connect to the Log Service
		StatLog.addDisplayer( new NLMISC::CStdDisplayer() );

		NLNET::CNetDisplayer *nd = new NLNET::CNetDisplayer;

		if ( nd->connected() )
		{
			StatLog.addDisplayer( nd );
		}
		else
		{
			nlerror( "Coudn't connect to the Log Service." );
		}
	}

};


NLNET_SERVICE_MAIN( CAdminExecutorService, "AES", 50009 );

// End of admin_executor_service.cpp