summaryrefslogtreecommitdiffstats
path: root/lib/VServer.pm
blob: b98e34af299fca63d050079c9c1dd92fe63600da (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/perl -T

package VServer;
our $VERSION = '1.12';

use strict;
use warnings;
use POSIX;

sub get_context_id_by_name($) {
    my $vserver = shift;
    if ($vserver =~ /^([-a-z0-9._]*)$/) {
        $vserver = $1;
    };

    my $dir = "/etc/vservers/$vserver";
    return unless -d $dir;

    open(my $context, '<', "$dir/context") || return undef;

    my $cid = undef;
    while (<$context>) {
        if ($_ =~ m/([0-9]*)/) {
            $cid = $1;
            last;
        }
    }

    close $context;
    return $cid;
}

sub get_context_id_by_dir($) {
    my $dir = shift;
    my $id = undef;

    open(my $info, "<", "$dir/info") || return undef;
    while (<$info>) {
        if ($_ =~ m/^ID:\s*([0-9.]+)$/) {
            $id = $1;
            last;
        }
    }
    close($info);

    return $id;
}

sub get_proc_dir($) {
    my $context = shift;
    my $dir = "/proc/virtual/$context/";

    return $dir if (-d $dir);
    return undef;
}

sub get_config_dir($) {
    my $context = shift;

    my $dir = qx(/usr/sbin/vuname --xid $context --get context 2> /dev/null);
    return undef unless ($? eq 0);

    chomp($dir);
    return $dir;
}

sub get_name($) {
    my $context = shift;
    my $dir = get_config_dir($context) || return undef;
    my $name = undef;

    open(my $file, "<", "$dir/name") || return undef;
    while (<$file>) {
        chomp($name = $_);
        last;
    }
    close($file);

    return $name;
}

sub get_loadavg($) {
    my $id = shift;
    my $dir = get_proc_dir($id) || return undef;
    my %load = ();

    open(my $cvirt, "<", "$dir/cvirt") || return undef;
    while (<$cvirt>) {
        if ($_ =~ m/^loadavg:\s*([0-9.]+)\s*([0-9.]+)\s*([0-9.]+)$/) {
            %load = (
                load1 => $1,
                load5 => $2,
                load15 => $3
            );
            last;
        }
    }
    close($cvirt);

    return \%load;
}

sub get_mem($) {
    my $context = shift;
    my $dir = get_proc_dir($context) || return undef;
    my $ps = POSIX::sysconf(&POSIX::_SC_PAGESIZE) || return undef;
    my %mem = ();

    open(my $limit, "<", "$dir/limit") || return undef;
    while (<$limit>) {
        if ($_ =~ m/^VM:\s*([0-9]+)\s/) {
            $mem{'vm'} = $1 * $ps;
        }
        elsif ($_ =~ m/RSS:\s*([0-9]+)\s/) {
            $mem{'rss'} = $1 * $ps;
        }
        elsif ($_ =~ m/ANON:\s*([0-9]+)\s/) {
            $mem{'anon'} = $1 * $ps;
        }
        elsif ($_ =~ m/VML:\s*([0-9]+)\s/) {
            $mem{'vml'} = $1 * $ps;
        }
    }
    close($limit);

    return \%mem;
}

sub get_net($) {
    my $context = shift;
    my $dir = get_proc_dir($context) || return undef;
    my %net = undef;

    open(my $cacct, "<", "$dir/cacct") || return undef;
    while (<$cacct>) {
        if ($_ =~ m/^UNIX:\s*[0-9]+\/([0-9]+)\s+[0-9]+\/([0-9]+)\s+[0-9]+\/([0-9]+)\s/) {
            $net{'unix'} = list($1, $2, $3);
        }
        elsif ($_ =~ m/^INET:\s*[0-9]+\/([0-9]+)\s+[0-9]+\/([0-9]+)\s+[0-9]+\/([0-9]+)\s/) {
            $net{'inet'} = list($1, $2, $3);
        }
        elsif ($_ =~ m/^INET6:\s*[0-9]+\/([0-9]+)\s+[0-9]+\/([0-9]+)\s+[0-9]+\/([0-9]+)\s/) {
            $net{'inet6'} = list($1, $2, $3);
        }
    }
    close($cacct);

    return \%net;
}

1;
__END__

=pod

=head1 NAME

VServer - little perl helpers for handling linux-vserver

=head1 SYNOPSIS

    use VServer;
    my $id = VServer::get_context_id("name");

=head1 DESCRIPTION

This module contains some functions for interfacing linux-vserver
with perl. It does not use the libvserver library, but reading files
in I</proc/virtual/> and I</etc/vservers/> and executing lightwight
external tools like I<vuname>.

=head2 Methods

=over 4

=item B<get_context_id_by_name($vserver_name)>

Returns the context id for a vserver given by its name. If an
invalid name is supplied or any other error occurs, B<undef>
is returned.

=item B<get_context_id_by_dir($proc_dir)>

Returns the context id for a vserver given by the path in the
I</proc/virtual/> tree. This could be done by parsing the path,
but the function uses the I<info> file and parsing the C<ID:> line.
If the I<info> file is not found or does not contain a line starting
with C<ID:>, B<undef> is returned.

=item B<get_proc_dir($context)>

Build the directory in C</proc/virtual/> for the given context id.
The directory is build with the following template:
I</proc/virtual/$context/>. If the resulting directory does not exist
(e.g. because the vserver is not running), B<undef> is returned.

=item B<get_config_dir($context)>

Returns the config directory for the given context id. This is done
by calling C</usr/sbin/vuname>. If any error occurs, B<undef> is
returned.

=item B<get_name($context)>

Returns the name of the vserver specified by the given context id.
This uses B<get_config_dir($context)> and reading the name file
inside the config directory. If any error occurs, B<undef> is
returned.

=item B<get_loadavg($context)>

Returns the three (load1, load5, load15) load values as hasref for
the vserver specified by the given context id. The functions reads
the B<cvirt> file inside the I</proc> directory of the vserver and
parses the loadavg values. The function returns a hashref or
B<undef> if any error occurs.

=item B<get_mem($context)>

Return memory information for the vserver specified by the given
context id. The function reads the B<limit> file inside the I</proc>
directory of the vserver, parses the memory information and
multiplies it with the pagesize. The function returns a hashref with
the following values or B<undef> if any error occurs:

=over

=item C<vm>

I<virtual memory>

=item C<rss>

I<resident set size>

=item C<anon>

I<anonymous memory>

=item C<vml>

I<virtual locked memory>

=back

=item B<get_net($context)>

Return socket information for the vserver specified by the given
context id. The function reads the B<cacct> file inside the I</proc>
directory of the vserver and parses the network information. The
function returns a hashref with an array of recieved, send and
error bytes for the follwoing values or B<undef> if any error
occurs:

=over

=item C<unix>

Bytes recieved, send and errored over unix domain sockets.

=item C<inet>

Bytes recieved, send and errored over IPv4 sockets.

=item C<inet6>

Bytes recieved, send and errored over IPv6 sockets.

=back

=back

=head1 AUTHORS

Alexander Sulfrian <alexander@sulfrian.net>

=cut