void printCtxt( const char* cmd ) { (void)cmd; if ( StatDirty ) processStat(); output( "%f\n", (float)( Ctxt / timeInterval ) ); }
void printCPUTotalLoad( const char* cmd ) { (void)cmd; if ( StatDirty ) processStat(); output( "%f\n", CPULoad.userLoad + CPULoad.sysLoad + CPULoad.niceLoad + CPULoad.waitLoad ); }
void printCPUIdle( const char* cmd ) { (void)cmd; if ( StatDirty ) processStat(); output( "%f\n", CPULoad.idleLoad ); }
void printCPUSys( const char* cmd ) { (void)cmd; if ( StatDirty ) processStat(); output( "%f\n", CPULoad.sysLoad ); }
void printCPUUser( const char* cmd ) { (void)cmd; if ( StatDirty ) processStat(); output( "%f\n", CPULoad.userLoad ); }
void printInterruptx( const char* cmd ) { int id; if ( StatDirty ) processStat(); sscanf( cmd + strlen( "cpu/interrupts/int" ), "%d", &id ); output( "%f\n", (float)( Intr[ id ] / timeInterval ) ); }
void printCPUxIdle( const char* cmd ) { int id; if ( StatDirty ) processStat(); sscanf( cmd + 7, "%d", &id ); output( "%f\n", SMPLoad[ id ].idleLoad ); }
void printCPUxTotalLoad( const char* cmd ) { int id; if ( StatDirty ) processStat(); sscanf( cmd + 7, "%d", &id ); output( "%f\n", SMPLoad[ id ].userLoad + SMPLoad[ id ].sysLoad + SMPLoad[ id ].niceLoad + SMPLoad[ id ].waitLoad ); }
void printCPUWait( const char* cmd ) { (void)cmd; if ( StatDirty ) processStat(); output( "%f\n", CPULoad.waitLoad ); }
void print24DiskWBlk( const char* cmd ) { int id; if ( StatDirty ) processStat(); sscanf( cmd + 9, "%d", &id ); /* a block is 512 bytes or 1/2 kBytes */ output( "%f\n", (float)( DiskLoad[ id ].s[ 4 ].delta / timeInterval * 2 ) ); }
void print24DiskWIO( const char* cmd ) { int id; if ( StatDirty ) processStat(); sscanf( cmd + 9, "%d", &id ); output( "%f\n", (float)( DiskLoad[ id ].s[ 2 ].delta / timeInterval ) ); }
void print24DiskIO( const char* cmd ) { int major, minor; char devname[DISKDEVNAMELEN]; char name[ 17 ]; DiskIOInfo* ptr; sscanf( cmd, "disk/%[^_]_(%d:%d)/%16s", devname, &major, &minor, name ); if ( StatDirty ) processStat(); ptr = DiskIO; while ( ptr && ( ptr->major != major || ptr->minor != minor ) ) ptr = ptr->next; if ( !ptr ) { print_error( "RECONFIGURE" ); output( "0\n" ); log_error( "Disk device disappeared" ); return; } if ( strcmp( name, "total" ) == 0 ) output( "%f\n", (float)( ptr->total.delta / timeInterval ) ); else if ( strcmp( name, "rio" ) == 0 ) output( "%f\n", (float)( ptr->rio.delta / timeInterval ) ); else if ( strcmp( name, "wio" ) == 0 ) output( "%f\n", (float)( ptr->wio.delta / timeInterval ) ); else if ( strcmp( name, "rblk" ) == 0 ) output( "%f\n", (float)( ptr->rblk.delta / ( timeInterval * 2 ) ) ); else if ( strcmp( name, "wblk" ) == 0 ) output( "%f\n", (float)( ptr->wblk.delta / ( timeInterval * 2 ) ) ); else { output( "0\n" ); log_error( "Unknown disk device property \'%s\'", name ); } }
void initStat( struct SensorModul* sm ) { /* The CPU load is calculated from the values in /proc/stat. The cpu * entry contains 7 counters. These counters count the number of ticks * the system has spend on user processes, system processes, nice * processes, idle and IO-wait time, hard and soft interrupts. * * SMP systems will have cpu1 to cpuN lines right after the cpu info. The * format is identical to cpu and reports the information for each cpu. * Linux kernels <= 2.0 do not provide this information! * * The /proc/stat file looks like this: * * cpu <user> <nice> <system> <idling> <waiting> <hardinterrupt> <softinterrupt> * disk 7797 0 0 0 * disk_rio 6889 0 0 0 * disk_wio 908 0 0 0 * disk_rblk 13775 0 0 0 * disk_wblk 1816 0 0 0 * page 27575 1330 * swap 1 0 * intr 50444 38672 2557 0 0 0 0 2 0 2 0 0 3 1429 1 7778 0 * ctxt 54155 * btime 917379184 * processes 347 * * Linux kernel >= 2.4.0 have one or more disk_io: lines instead of * the disk_* lines. * * Linux kernel >= 2.6.x(?) have disk I/O stats in /proc/diskstats * and no disk relevant lines are found in /proc/stat */ char format[ 32 ]; char tagFormat[ 16 ]; char buf[ 1024 ]; char tag[ 32 ]; StatSM = sm; sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 ); sprintf( tagFormat, "%%%ds", (int)sizeof( tag ) - 1 ); FILE *stat = fopen("/proc/stat", "r"); if(!stat) { print_error( "Cannot open file \'/proc/stat\'!\n" "The kernel needs to be compiled with support\n" "for /proc file system enabled!\n" ); return; } while ( fscanf( stat, format, buf ) == 1 ) { buf[ sizeof( buf ) - 1 ] = '\0'; sscanf( buf, tagFormat, tag ); if ( strcmp( "cpu", tag ) == 0 ) { /* Total CPU load */ registerMonitor( "cpu/system/user", "float", printCPUUser, printCPUUserInfo, StatSM ); registerMonitor( "cpu/system/nice", "float", printCPUNice, printCPUNiceInfo, StatSM ); registerMonitor( "cpu/system/sys", "float", printCPUSys, printCPUSysInfo, StatSM ); registerMonitor( "cpu/system/TotalLoad", "float", printCPUTotalLoad, printCPUTotalLoadInfo, StatSM ); registerMonitor( "cpu/system/idle", "float", printCPUIdle, printCPUIdleInfo, StatSM ); registerMonitor( "cpu/system/wait", "float", printCPUWait, printCPUWaitInfo, StatSM ); /* Monitor names changed from kde3 => kde4. Remain compatible with legacy requests when possible. */ registerLegacyMonitor( "cpu/user", "float", printCPUUser, printCPUUserInfo, StatSM ); registerLegacyMonitor( "cpu/nice", "float", printCPUNice, printCPUNiceInfo, StatSM ); registerLegacyMonitor( "cpu/sys", "float", printCPUSys, printCPUSysInfo, StatSM ); registerLegacyMonitor( "cpu/TotalLoad", "float", printCPUTotalLoad, printCPUTotalLoadInfo, StatSM ); registerLegacyMonitor( "cpu/idle", "float", printCPUIdle, printCPUIdleInfo, StatSM ); registerLegacyMonitor( "cpu/wait", "float", printCPUWait, printCPUWaitInfo, StatSM ); } else if ( strncmp( "cpu", tag, 3 ) == 0 ) { char cmdName[ 24 ]; /* Load for each SMP CPU */ int id; sscanf( tag + 3, "%d", &id ); CPUCount++; sprintf( cmdName, "cpu/cpu%d/user", id ); registerMonitor( cmdName, "float", printCPUxUser, printCPUxUserInfo, StatSM ); sprintf( cmdName, "cpu/cpu%d/nice", id ); registerMonitor( cmdName, "float", printCPUxNice, printCPUxNiceInfo, StatSM ); sprintf( cmdName, "cpu/cpu%d/sys", id ); registerMonitor( cmdName, "float", printCPUxSys, printCPUxSysInfo, StatSM ); sprintf( cmdName, "cpu/cpu%d/TotalLoad", id ); registerMonitor( cmdName, "float", printCPUxTotalLoad, printCPUxTotalLoadInfo, StatSM ); sprintf( cmdName, "cpu/cpu%d/idle", id ); registerMonitor( cmdName, "float", printCPUxIdle, printCPUxIdleInfo, StatSM ); sprintf( cmdName, "cpu/cpu%d/wait", id ); registerMonitor( cmdName, "float", printCPUxWait, printCPUxWaitInfo, StatSM ); } else if ( strcmp( "disk", tag ) == 0 ) { unsigned long val; char* b = buf + 5; /* Count the number of registered disks */ for ( DiskCount = 0; *b && sscanf( b, "%lu", &val ) == 1; DiskCount++ ) { while ( *b && isblank( *b++ ) ); while ( *b && isdigit( *b++ ) ); } if ( DiskCount > 0 ) DiskLoad = (DiskLoadInfo*)malloc( sizeof( DiskLoadInfo ) * DiskCount ); initStatDisk( tag, buf, "disk", "disk", 0, print24DiskTotal, print24DiskTotalInfo ); } else if ( initStatDisk( tag, buf, "disk_rio", "rio", 1, print24DiskRIO, print24DiskRIOInfo ) ); else if ( initStatDisk( tag, buf, "disk_wio", "wio", 2, print24DiskWIO, print24DiskWIOInfo ) ); else if ( initStatDisk( tag, buf, "disk_rblk", "rblk", 3, print24DiskRBlk, print24DiskRBlkInfo ) ); else if ( initStatDisk( tag, buf, "disk_wblk", "wblk", 4, print24DiskWBlk, print24DiskWBlkInfo ) ); else if ( strcmp( "disk_io:", tag ) == 0 ) process24DiskIO( buf ); else if ( strcmp( "page", tag ) == 0 ) { sscanf( buf + 5, "%lu %lu", &OldPageIn, &OldPageOut ); registerMonitor( "cpu/pageIn", "float", printPageIn, printPageInInfo, StatSM ); registerMonitor( "cpu/pageOut", "float", printPageOut, printPageOutInfo, StatSM ); } else if ( strcmp( "intr", tag ) == 0 ) { unsigned int i; char cmdName[ 32 ]; char* p = buf + 5; /* Count the number of listed values in the intr line. */ NumOfInts = 0; while ( *p ) if ( *p++ == ' ' ) NumOfInts++; /* It looks like anything above 24 is always 0. So let's just * ignore this for the time being. */ if ( NumOfInts > 25 ) NumOfInts = 25; OldIntr = (unsigned long*)malloc( NumOfInts * sizeof( unsigned long ) ); Intr = (unsigned long*)malloc( NumOfInts * sizeof( unsigned long ) ); i = 0; p = buf + 5; for ( i = 0; p && i < NumOfInts; i++ ) { sscanf( p, "%lu", &OldIntr[ i ] ); while ( *p && *p != ' ' ) p++; while ( *p && *p == ' ' ) p++; sprintf( cmdName, "cpu/interrupts/int%02d", i ); registerMonitor( cmdName, "float", printInterruptx, printInterruptxInfo, StatSM ); } } else if ( strcmp( "ctxt", tag ) == 0 ) { sscanf( buf + 5, "%lu", &OldCtxt ); registerMonitor( "cpu/context", "float", printCtxt, printCtxtInfo, StatSM ); } } fclose(stat); stat = fopen("/proc/vmstat", "r"); if(!stat) { print_error( "Cannot open file \'/proc/vmstat\'\n"); } else { while ( fscanf( stat, format, buf ) == 1 ) { buf[ sizeof( buf ) - 1 ] = '\0'; sscanf( buf, tagFormat, tag ); if ( strcmp( "pgpgin", tag ) == 0 ) { sscanf( buf + 7, "%lu", &OldPageIn ); registerMonitor( "cpu/pageIn", "float", printPageIn, printPageInInfo, StatSM ); } else if ( strcmp( "pgpgout", tag ) == 0 ) { sscanf( buf + 7, "%lu", &OldPageOut ); registerMonitor( "cpu/pageOut", "float", printPageOut, printPageOutInfo, StatSM ); } } fclose(stat); } if ( CPUCount > 0 ) SMPLoad = (CPULoadInfo*)calloc( CPUCount, sizeof( CPULoadInfo ) ); /* Call processStat to eliminate initial peek values. */ processStat(); }
/** * Processes all the POP3 commands such as * USER, PASS, LIST, RETR, DELE, NOOP, RSET, STAT */ int processPOPCommands(int sockfd, char *msg) { char *clientMsg = NULL; clientMsg = (char *)malloc(sizeof(char *) * 1024); if (clientMsg == NULL) { perror("ERROR: Cannot allocate memory"); return(-1); } if (strncasecmp(msg, POP_SSL_STARTTLS, strlen(POP_SSL_STARTTLS)) == 0) { //EHLO if (setupSSLCommunication(sockfd)) { writeClient(sockfd, "OK Begin TLS negotiation now", TRUE); } } else if (strncasecmp(msg, POP_USER, strlen(POP_USER)) == 0) { //USER if (checkPOPStates(iPOP_USER)) { if (processUser(msg)== -1) { strcpy(clientMsg,"User name required"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } else writeClient(sockfd, POP_OK, TRUE); } else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else if (strncasecmp(msg, POP_PASS, strlen(POP_PASS)) == 0) { //PASS if (checkPOPStates(iPOP_PASS)) { if (processPassword(msg) == -1) { strcpy(clientMsg,"Password required"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } else { struct userdata *user; if (fetch_user_data(username, password, &user)) { //VALID USER //GET MESSAGE LISTING FOR THE USER getListMessages(username); writeClient(sockfd, POP_OK, TRUE); free(user); } else { strcpy(clientMsg,"Invalid User"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } } else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else if (strncasecmp(msg, POP_LIST, strlen(POP_LIST)) == 0) { //LIST if (checkPOPStates(iPOP_LIST)) processList(sockfd, msg); else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else if (strncasecmp(msg, POP_RETR, strlen(POP_RETR)) == 0) { //RETR if (checkPOPStates(iPOP_RETR)) { if (processRetr(sockfd, msg) == -1) { strcpy(clientMsg,"Invalid Syntax"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else if (strncasecmp(msg, POP_DELE, strlen(POP_DELE)) == 0) { //DELE if (checkPOPStates(iPOP_DELE)) { if (processDelete(sockfd, msg)== -1) { strcpy(clientMsg,"Invalid Syntax"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else if (strncasecmp(msg, POP_RSET, strlen(POP_RSET)) == 0) { //RSET // if (checkStates(iPOP_RSET)) // processRset(sockfd, msg); // else // writeClient(sockfd, POP_RESP_503, TRUE); } else if (strncasecmp(msg, POP_NOOP, strlen(POP_NOOP)) == 0) { //NOOP if (checkPOPStates(iPOP_NOOP)) writeClient(sockfd, POP_OK, TRUE); else writeClient(sockfd, POP_ERR, TRUE); } else if (strncasecmp(msg, POP_STAT, strlen(POP_STAT)) == 0) { //STAT if (checkPOPStates(iPOP_STAT)) { processStat(sockfd, msg); } else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd, getMessage(TRUE, &clientMsg), TRUE); } } else { strcpy(clientMsg,"Invalid State"); writeClient(sockfd,getMessage(TRUE, &clientMsg), TRUE); } free(clientMsg); return(0); }