Debian Codegraph @

plugins/loadcheck

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 loadcheck
6
7 =head1 DESCRIPTION
8
9 Only takes email transactions if the system load is at or below a
10 specified level.
11
12 If this is running on a system that provides /kern/loadavg or
13 /proc/loadavg it will be used instead of the 'uptime' command.
14
15 Once a load value is determined, it is cached for a period of time.
16 See the cache_time below.
17
18 Since fork/exec is expensive in perl, if using the 'uptime' method,
19 use cache_time to avoid increasing your load on every connection.
20
21 =head1 CONFIG
22
23 max_load
24
25 This is the 1 minute system load where we won't take transactions
26 if our load is higher than this value. (Default: 7)
27
28 cache_time
29
30 A recently determined load value will be cached and used for the
31 assigned number of seconds. (Default: 10)
32
33 uptime
34
35 The path to the command 'uptime' if different than the default.
36 (Default: /usr/bin/uptime)
37
38 Example:
39
40 loadcheck cache_time 30
41
42 loadcheck max_load 7 uptime /usr/bin/uptime
43
44 =head1 SEE ALSO
45
46 Original version: http://www.nntp.perl.org/group/perl.qpsmtpd/2006/01/msg4422.html
47
48 Variant with caching: http://www.nntp.perl.org/group/perl.qpsmtpd/2006/03/msg4710.html
49
50 Steve Kemp's announcement of an alternate load limiter: http://www.nntp.perl.org/group/perl.qpsmtpd/2008/03/msg7814.html
51
52 =head1 AUTHOR
53
54 Written by Peter Eisch <[email protected]>.
55
56 =head1 CHANGES
57
58 v0.03 - msimerson - 2014-03-21
59
60 * refactored "find the way to get load avg" out of loadcheck (every
61 connection) into get_load_method which is run in register. If we can't
62 get the load average, don't register the hook.
63
64 * added BSD::getloadavg method (tested on FreeBSD)
65
66 v0.02 - github@rsiddall - resurrected from list archives
67
68 =cut
69
70 my $VERSION = 0.03;
71
72 sub register {
73 my ($self, $qp, @args) = @_;
74
75 $self->{_args} = { @args };
76
77 $self->{_args}{max_load} ||= 7;
78 $self->{_args}{uptime} ||= '/usr/bin/uptime';
79 $self->{_args}{cache_time} ||= 10;
80 $self->{_load} = -1;
81 $self->{_time} = 0;
82 $self->{_method} = $self->get_load_method();
83
84 # only register the hook if we can measure load
85 if (ref $self->{_method} eq 'CODE') {
86 $self->register_hook("connect", "loadcheck");
87 }
88 }
89
90 sub loadcheck {
91 my ($self, $transaction) = @_;
92
93 if (time() > ($self->{_time} + $self->{_args}{cache_time})) {
94 # cache value expired, update
95 $self->{_method}->();
96 $self->{_time} = time();
97 };
98
99 if ($self->{_load} > $self->{_args}{max_load}) {
100 $self->log(LOGERROR, "local load too high: $self->{_load}");
101 return (DENYSOFT, "Server load too high, please try again later.");
102 }
103
104 return (DECLINED, "continuing with load: $self->{_load}");
105 }
106
107 sub get_load_method {
108 my ($self) = @_;
109
110 eval "use BSD::getloadavg;";
111 if (!$@) {
112 return sub {
113 require BSD::getloadavg;
114 $self->{_load} = (getloadavg())[0];
115 $self->log(LOGDEBUG, "BSD::getloadavg reported: $self->{_load}");
116 }
117 }
118
119 if (-r '/kern/loadavg') { # *BSD
120 return sub {
121 open(LD, '<', "/kern/loadavg"); # contains fix-point scaling value
122 my $res = <LD>;
123 close LD;
124 my @vals = split(/ /, $res);
125 $self->{_load} = ($vals[0] / $vals[3]);
126 $self->log(LOGDEBUG, "/kern/loadavg reported: $self->{_load}");
127 }
128 }
129
130 if (-r '/proc/loadavg') { # *inux
131 return sub {
132 open(LD, "<", "/proc/loadavg"); # contains decimal value
133 my $res = <LD>; # contains fix-point scaling value
134 close LD;
135 $self->{_load} = (split(/ /, $res))[0];
136 $self->log(LOGDEBUG, "/proc/loadavg reported: $self->{_load}");
137 }
138 }
139
140 if (-x $self->{_args}{uptime}) {
141 return sub {
142 # the various formats returned:
143 # 10:33AM up 2:06, 1 user, load averages: 6.55, 3.76, 2.48
144 # 12:29am 2 users, load average: 0.05, 0.05, 0.06
145 # 12:30am up 5 days, 12:43, 1 user, load average: 0.00, 0.00, 0.00
146
147 my $res = `$self->{_args}{uptime}`;
148 if ($res =~ /aver\S+: (\d+\.\d+)/) {
149 $self->{_load} = $1;
150 $self->log(LOGDEBUG, "$self->{_args}{uptime} reported: $self->{_load}");
151 }
152 }
153 }
154
155 $self->log(LOGERROR, "unable to acquire system load");
156 return;
157 };
158