summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/Linux/loadavg.c
blob: 788e32793b217e6206e03b5ec65ee799c7cd53d6 (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
/*
    KSysGuard, the KDE System Guard

    Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

#include "ksysguardd.h"
#include "Command.h"

#include "loadavg.h"

static int LoadAvgOK = 0;
static double LoadAvg1, LoadAvg5, LoadAvg15;

#define LOADAVGBUFSIZE 128
static char LoadAvgBuf[ LOADAVGBUFSIZE ];
static int Dirty = 0;

static void processLoadAvg( void )
{
  sscanf( LoadAvgBuf, "%lf %lf %lf", &LoadAvg1, &LoadAvg5, &LoadAvg15 );
  Dirty = 0;
}

/*
================================ public part =================================
*/

void initLoadAvg( struct SensorModul* sm )
{
  if ( updateLoadAvg() < 0 ) {
    LoadAvgOK = -1;
    return;
  } else
    LoadAvgOK = 1;

  registerMonitor( "cpu/loadavg1", "float", printLoadAvg1, printLoadAvg1Info, sm );
  registerMonitor( "cpu/loadavg5", "float", printLoadAvg5, printLoadAvg5Info, sm );
  registerMonitor( "cpu/loadavg15", "float", printLoadAvg15, printLoadAvg15Info, sm );
}

void exitLoadAvg( void )
{
  LoadAvgOK = -1;
}

int updateLoadAvg( void )
{
  size_t n;
  int fd;

  if ( LoadAvgOK < 0 )
    return -1;

  if ( ( fd = open( "/proc/loadavg", O_RDONLY ) ) < 0 ) {
    if ( LoadAvgOK != 0 )
      print_error( "Cannot open file \'/proc/loadavg\'!\n"
                   "The kernel needs to be compiled with support\n"
                   "for /proc filesystem enabled!\n" );
    return -1;
  }

  if ( ( n = read( fd, LoadAvgBuf, LOADAVGBUFSIZE - 1 ) ) == LOADAVGBUFSIZE - 1 ) {
    log_error( "Internal buffer too small to read \'/proc/loadavg\'" );

    close( fd );
    return -1;
  }

  close( fd );
  LoadAvgBuf[ n ] = '\0';
  Dirty = 1;

  return 0;
}

void printLoadAvg1( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processLoadAvg();

  fprintf( CurrentClient, "%f\n", LoadAvg1 );
}

void printLoadAvg1Info( const char* cmd )
{
  (void)cmd;
  fprintf( CurrentClient, "Load average 1 min\t0\t0\t\n" );
}

void printLoadAvg5( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processLoadAvg();

  fprintf( CurrentClient, "%f\n", LoadAvg5 );
}

void printLoadAvg5Info( const char* cmd )
{
  (void)cmd;
  fprintf( CurrentClient, "Load average 5 min\t0\t0\t\n" );
}

void printLoadAvg15( const char* cmd )
{
  (void)cmd;

  if ( Dirty )
    processLoadAvg();

  fprintf( CurrentClient, "%f\n", LoadAvg15 );
}

void printLoadAvg15Info( const char* cmd )
{
  (void)cmd;
  fprintf( CurrentClient, "Load average 15 min\t0\t0\t\n" );
}