summaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/image.c25
-rw-r--r--src/server/image.h4
-rw-r--r--src/server/net.c13
-rw-r--r--src/server/net.h2
-rw-r--r--src/server/rpc.c24
-rw-r--r--src/server/server.c1
-rw-r--r--src/server/uplink.c12
-rw-r--r--src/server/uplink.h2
8 files changed, 35 insertions, 48 deletions
diff --git a/src/server/image.c b/src/server/image.c
index f552843..920f15d 100644
--- a/src/server/image.c
+++ b/src/server/image.c
@@ -1133,31 +1133,30 @@ bool image_generateCrcFile(char *image)
return true;
}
-void image_printAll()
+json_t* image_fillJson()
{
- int i, percent, pending, j;
+ json_t *imagesJson = json_array();
+ json_t *image;
+
+ int i;
char buffer[100] = { 0 };
spin_lock( &_images_lock );
for (i = 0; i < _num_images; ++i) {
if ( _images[i] == NULL ) continue;
spin_lock( &_images[i]->lock );
- logadd( LOG_DEBUG1, "Image: %s", _images[i]->lower_name );
- percent = image_getCompletenessEstimate( _images[i] );
- logadd( LOG_DEBUG1, " |- Complete: %d%%", percent );
+ image = json_pack( "{sssIsI}", "image", _images[i]->lower_name, "users", (json_int_t) _images[i]->users,
+ "complete", (json_int_t) image_getCompletenessEstimate( _images[i] ) );
if ( _images[i]->uplink != NULL ) {
host_to_string( &_images[i]->uplink->currentServer, buffer, sizeof(buffer) );
- pending = 0;
- spin_lock( &_images[i]->uplink->queueLock );
- for (j = 0; j < _images[i]->uplink->queueLen; ++j) {
- if ( _images[i]->uplink->queue[j].status != ULR_FREE ) pending++;
- }
- spin_unlock( &_images[i]->uplink->queueLock );
- logadd( LOG_DEBUG1, " |- Uplink: %s -- %d pending requests", buffer, pending );
+ json_object_set_new( image, "uplinkServer", json_string( buffer ) );
+ json_object_set_new( image, "receivedBytes", json_integer( (json_int_t) _images[i]->uplink->bytesReceived ) );
}
- logadd( LOG_DEBUG1, " `- Users: %d\n", _images[i]->users );
+ json_array_append_new( imagesJson, image );
+
spin_unlock( &_images[i]->lock );
}
spin_unlock( &_images_lock );
+ return imagesJson;
}
/**
diff --git a/src/server/image.h b/src/server/image.h
index aca3788..c7e2b6f 100644
--- a/src/server/image.h
+++ b/src/server/image.h
@@ -1,6 +1,8 @@
#ifndef _IMAGE_H_
#define _IMAGE_H_
+#include <jansson.h>
+
#include "../config.h"
#include "globals.h"
@@ -38,7 +40,7 @@ bool image_create(char *image, int revision, uint64_t size);
bool image_generateCrcFile(char *image);
-void image_printAll();
+json_t* image_fillJson();
int image_getCompletenessEstimate(const dnbd3_image_t * const image);
diff --git a/src/server/net.c b/src/server/net.c
index dc53701..9dc1442 100644
--- a/src/server/net.c
+++ b/src/server/net.c
@@ -124,14 +124,6 @@ uint64_t net_getTotalBytesSent()
return tmp;
}
-void net_addTotalBytesSent(uint64_t sentBytes)
-{
- spin_lock( &statisticsSentLock );
- totalBytesSent += sentBytes;
- spin_unlock( &statisticsSentLock );
- return;
-}
-
void net_init()
{
spin_init( &statisticsSentLock, PTHREAD_PROCESS_PRIVATE );
@@ -206,7 +198,6 @@ void *net_client_handler(void *dnbd3_client)
}
} else if ( strncmp( (char*)&request, "GET ", 4 ) == 0 || strncmp( (char*)&request, "POST ", 5 ) == 0 ) {
rpc_sendStatsJson( client->sock );
- logadd( LOG_INFO, "Sending statistics." );
}
if ( bOk ) {
@@ -388,7 +379,9 @@ set_name: ;
}
exit_client_cleanup: ;
dnbd3_removeClient( client );
- net_addTotalBytesSent( client->bytesSent ); // Add the amount of bytes sent by the client to the statistics of the server.
+ spin_lock( &statisticsSentLock );
+ totalBytesSent += client->bytesSent;// Add the amount of bytes sent by the client to the statistics of the server.
+ spin_unlock( &statisticsSentLock );
client = dnbd3_freeClient( client );
return NULL ;
}
diff --git a/src/server/net.h b/src/server/net.h
index e8bef34..4624141 100644
--- a/src/server/net.h
+++ b/src/server/net.h
@@ -25,8 +25,6 @@ void net_init();
uint64_t net_getTotalBytesSent();
-void net_addTotalBytesSent(uint64_t sentBytes);
-
void *net_client_handler(void *client_socket);
#endif /* NET_H_ */
diff --git a/src/server/rpc.c b/src/server/rpc.c
index ae01a0e..af3507c 100644
--- a/src/server/rpc.c
+++ b/src/server/rpc.c
@@ -1,7 +1,3 @@
-#include <unistd.h>
-#include <string.h>
-#include <jansson.h>
-
#include "rpc.h"
#include "server.h"
#include "net.h"
@@ -9,6 +5,12 @@
#include "log.h"
#include "locks.h"
#include "helper.h"
+#include "image.h"
+
+#include <unistd.h>
+#include <string.h>
+#include <jansson.h>
+#include <inttypes.h>
static void clientsToJson(json_t *jsonClients);
@@ -16,12 +18,16 @@ void rpc_sendStatsJson(int sock)
{
uint64_t receivedBytes = uplink_getTotalBytesReceived();
uint64_t sentBytes = net_getTotalBytesSent();
+ int uptime = dnbd3_serverUptime();
json_t *jsonClients = json_array();
clientsToJson( jsonClients );
- json_t *statisticsJson = json_pack( "{sisi}", "receivedBytes", (json_int_t) receivedBytes, "sentBytes", (json_int_t) sentBytes );
- json_object_set( statisticsJson, "clients", jsonClients );
+
+ json_t *statisticsJson = json_pack( "{sIsI}", "receivedBytes", (json_int_t) receivedBytes, "sentBytes", (json_int_t) sentBytes );
+ json_object_set_new( statisticsJson, "clients", jsonClients );
+ json_object_set_new( statisticsJson, "images", image_fillJson() );
+ json_object_set_new( statisticsJson, "uptime", json_integer( uptime ) );
char *jsonString = json_dumps( statisticsJson, 0 );
char buffer[500];
@@ -30,7 +36,6 @@ void rpc_sendStatsJson(int sock)
write( sock, buffer, strlen( buffer ) );
write( sock, jsonString, strlen( jsonString ) );
json_decref( statisticsJson );
- json_decref( jsonClients );
free( jsonString );
}
@@ -46,9 +51,8 @@ static void clientsToJson(json_t *jsonClients)
spin_lock( &_clients[i]->lock );
host_to_string( &_clients[i]->host, clientName, sizeof(clientName) );
imageName =_clients[i]->image != NULL ? _clients[i]->image->lower_name : "NULL";
- clientStats = json_pack( "{sssssi}", "client", clientName, "image", imageName , "bytesSent", (json_int_t) _clients[i]->bytesSent );
- json_array_append( jsonClients, clientStats );
- json_decref( clientStats );
+ clientStats = json_pack( "{sssssI}", "client", clientName, "image", imageName , "bytesSent", (json_int_t)_clients[i]->bytesSent );
+ json_array_append_new( jsonClients, clientStats );
spin_unlock( &_clients[i]->lock );
}
spin_unlock( &_clients_lock );
diff --git a/src/server/server.c b/src/server/server.c
index a5bfc3e..6a317fe 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -59,7 +59,6 @@ static bool sigReload = false, sigLogCycle = false;
static bool dnbd3_addClient(dnbd3_client_t *client);
static void dnbd3_handleSignal(int signum);
-static void dnbd3_printClients();
/**
* Print help text for usage instructions
diff --git a/src/server/uplink.c b/src/server/uplink.c
index d76f030..e1c776b 100644
--- a/src/server/uplink.c
+++ b/src/server/uplink.c
@@ -48,14 +48,6 @@ uint64_t uplink_getTotalBytesReceived()
return tmp;
}
-void uplink_addTotalBytesReceived(int receivedBytes)
-{
- spin_lock( &statisticsReceivedLock );
- totalBytesReceived += receivedBytes;
- spin_unlock( &statisticsReceivedLock );
- return;
-}
-
/**
* Create and initialize an uplink instance for the given
* image. Uplinks run in their own thread.
@@ -452,7 +444,9 @@ static void* uplink_mainloop(void *data)
spin_destroy( &link->queueLock );
free( link->recvBuffer );
link->recvBuffer = NULL;
- uplink_addTotalBytesReceived( link->bytesReceived );
+ spin_lock( &statisticsReceivedLock );
+ totalBytesReceived += link->bytesReceived;
+ spin_unlock( &statisticsReceivedLock );
free( link );
return NULL ;
}
diff --git a/src/server/uplink.h b/src/server/uplink.h
index c0a68de..43c068f 100644
--- a/src/server/uplink.h
+++ b/src/server/uplink.h
@@ -8,8 +8,6 @@ void uplink_globalsInit();
uint64_t uplink_getTotalBytesReceived();
-void uplink_addTotalBytesReceived(int receivedBytes);
-
bool uplink_init(dnbd3_image_t *image, int sock, dnbd3_host_t *host);
void uplink_removeClient(dnbd3_connection_t *uplink, dnbd3_client_t *client);