aboutsummaryrefslogtreecommitdiff
path: root/setup/mysqlstat-setup.pl
blob: eb5408463401349776ceb0ef8d0d6c207a7b5fd4 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/perl

use strict;
use DBI;
use Pod::Usage;
use Pod::Man;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use File::Basename;
use File::Temp qw{tempfile};

=head1 NAME

mysqlstat-setup - sets up mysqlstat Net-SNMP module
    
=head1 SYNOPSIS

B<mysqlstat-setup>
[B<-f>]    
[B<-u> I<USER>]
[B<-p> I<PASSWORD>]
[B<-C> I<CONFDIR>]    
[B<-L> I<LIBDIR>]    
[B<--defaults-file=>I<FILE>]
[B<--force>]    
[B<--host=>I<HOST>]
[B<--user=>I<USER>]    
[B<--password=>I<PASSWORD>]
[B<--snmp-user=>I<USER>]
[B<--snmp-password=>I<PASSWORD>]    
[B<--confdir> I<CONFDIR>]    
[B<--libdir> I<LIBDIR>]    
    
B<mysqlstat-setup>
[B<-h>]
[B<--help>]
[B<--usage>]    
    
=head1 DESCRIPTION

=head1 OPTIONS    
    
=cut

my $progname = basename($0);
my $progdescr = 'Set up mysqlstat module';

use constant EX_OK  => 0;
use constant EX_ERR => 1;

my $libdir = q{@DLMODDIR@};   # Directory where mysqlstat.so lives.
my $confdir = q{@CONFDIR@};   # SNMP configuration directory. 

my $mysql_user;           # MySQL administrator user
my $mysql_password;       # His password 
my $mysql_defaults_file;  # Options file to use (default ~/.my.cnf)
my $mysql_host;           # Host name or IP
my $mysql_port;           # Port the server is listening on.

my $snmp_user = 'snmp';       # MySQL user the plugin will be using.
my $snmp_password;            # MySQL password for $snmp_user
my $snmp_host = 'localhost';  # Host to use 
my $force;                # Force setup even if $confdir/mysqlstat.cnf is found.

my $sofile;               # Full pathname of mysqlstat.so   
my $mysqlstat_cnf;        # Full pathname of mysqlstat.cnf

my $owner_uid;            # Owner UID ...
my $owner_gid;            # ... and GID for the mysqlstat.cnf file.

sub error {
    my $text = shift;
    print STDERR "$progname: $text\n";
    if (@_) {
	print STDERR "\n";
	foreach $text (@_) {
	    print STDERR "  $text\n";
	}
	print STDERR "\n";	
    }
}

sub db_connect {
    my $arg;
    $arg .= ":host=$mysql_host" if defined $mysql_host;
    $arg .= ":port=$mysql_port" if defined $mysql_port;
    $arg .= ":;mysql_read_default_file=$mysql_defaults_file"
        if defined $mysql_defaults_file;
    if (!$arg
        or (!defined($mysql_user) and !defined($mysql_defaults_file))) {
        my $my_cnf = "$ENV{HOME}/.my.cnf";
        if (-r $my_cnf) {
            error("info: using mysql option file $my_cnf");
            $arg .= ":;mysql_read_default_file=$my_cnf";
        }
    }
    $arg = 'DBI:mysql'.$arg;

    my $dbd = DBI->connect($arg, $mysql_user, $mysql_password,
			   { PrintError => 0, AutoCommit => 1 });
    unless ($dbd) {
	error("can't connect to MySQL server: ".$DBI::errstr);
	exit EX_ERR;
    }
    return $dbd;
}

# db_modify(DB, QUERY, ARGS)
sub db_modify {
    my $dbd = shift;
    my $q = shift;
    my $sth = $dbd->prepare($q);
    my $res = $sth->execute(@_) or croak($sth->errstr);
    $sth->finish;
    return $res;
}

sub sql_create_user {
    my $dbd = db_connect();
    my $q = q{GRANT PROCESS, REPLICATION CLIENT ON *.* TO ?@?};
    my @args = ($snmp_user, $snmp_host);
    if ($snmp_password) {
	push @args, $snmp_password;
	$q .= ' IDENTIFIED BY ?';
    }
    db_modify($dbd, $q, @args);
    db_modify($dbd, 'FLUSH PRIVILEGES');
    $dbd->disconnect;
}

sub edit_cnf {
    my $m = umask(077);
    open(my $fd, '>', $mysqlstat_cnf)
	or die "can't open $mysqlstat_cnf for writing: $!";
    print $fd "[mysqlstat]\n";
    print $fd "user=$snmp_user\n";
    print $fd "password=$snmp_password\n" if $snmp_password;
    print $fd "host=$snmp_host\n" if $snmp_host and $snmp_host ne 'localhost';
    chown $owner_uid, $owner_gid, $fd;
    close $fd;
    umask($m);
}

sub backup {
    my $name = shift;
    my $bkname = "${name}~";
    backup($bkname) if -f $bkname;
    unless (rename($name, $bkname)) {
	error("can't create backup file $bkname: $!");
	return 0;
    }
    return 1;
}

sub edit_snmp_conf {
    my $snmpd_conf = "$confdir/snmpd.conf";
    $snmpd_conf = readlink $snmpd_conf if -l $snmpd_conf;

    open(my $fd, '<', $snmpd_conf)
	or die "can't open $snmpd_conf for reading: $!";
    
    my $line = 0;
    my $dlmod_line;
    while (<$fd>) {
	++$line;
	chomp;
	s/^\s+//;
	next if /^#/;
	if (/^dlmod\s/) {
	    my @a = split /\s+/;
	    if ($a[$#a] eq $sofile) {
		error("info: $sofile already loaded in $snmpd_conf:$line");
		return 1;
	    }
	    $dlmod_line = $line;
	}
    }
    seek $fd, 0, 0;
    
    my ($ofd, $filename) = tempfile('snmpd.XXXXXX', DIR => $confdir);
    if ($dlmod_line) {
	$line = 0;
	while (<$fd>) {
	    print $ofd $_;
	    if (++$line == $dlmod_line) {
		print $ofd "# This line was added by $progname\n";
		print $ofd "dlmod mysqlstat_mib $sofile\n";
	    }
	}
    } else {
	while (<$fd>) {
	    print $ofd $_;
	}
	print $ofd "# This line was added by $progname\n";
	print $ofd "dlmod mysqlstat_mib $sofile\n";
    }
    close $fd;
    close $ofd;

    unless (backup($snmpd_conf)) {
	error("can't preserve backup copy of $snmpd_conf");
	error("updated configuration left in file $filename");
    } elsif (not rename($filename, $snmpd_conf)) {
	error("failed to rename $filename to $snmpd_conf");
	error("updated configuration left in file $filename");
    }
}

# 
GetOptions("h" => sub {
               pod2usage(-message => "$progname: $progdescr",
		         -exitstatus => EX_OK);
	   },
	   "help" => sub {
	       pod2usage(-exitstatus => EX_OK, -verbose => 2);
	   },
	   "usage" => sub {
	       pod2usage(-exitstatus => EX_OK, -verbose => 0);
	   },
	   "user|u=s" => \$mysql_user,
	   "password|p=s" => \$mysql_password,
	   "defaults-file=s" => \$mysql_defaults_file,
	   "host=s" => \$mysql_host,
	   "snmp-user=s" => \$snmp_user,
	   "snmp-password=s" => \$snmp_password,
	   "snmp-host=s" => \$snmp_host,
	   "confdir|C=s" => \$confdir,
	   "libdir|L=s" => \$libdir,
	   "force|f" => \$force
    ) or exit(EX_ERR);

unless ($libdir =~ m#^/#) {
    error('NetSNMP library directory is not set',
      "It appears you invoked a copy of $progname that hasn't been installed.",
      "If you are confident in what you're doing, then please use",
      "the --libdir=DIR option to inform the program about the location of the",
      "library directory.  Otherwise, please use the program that has been",
      "installed properly.");
    exit EX_ERR;
}

unless ($confdir =~ m#^/#) {
    error('NetSNMP configuration directory is not set',
      "It appears you invoked a copy of $progname that hasn't been installed.",
      "If you are confident in what you're doing, then please use",
      "the --confdir=DIR option to inform the program about the location of the",
      "configuration directory.  Otherwise, please use the program that has been",
      "installed properly.");
    exit EX_ERR;
}

$sofile = "$libdir/mysqlstat.so";

unless (-f $sofile) {
    error("the library $sofile is not found");
    exit EX_ERR;
}

$mysqlstat_cnf = "$confdir/mysqlstat.cnf";
if (-e $mysqlstat_cnf and not $force) {
    error("file $mysqlstat_cnf already exists",
	  "If you are sure you wan't to re-setup mysqlstat, use the --force option, e.g.:",
	  "  $progname --force");
    exit EX_ERR;
}

unless ($< == 0) {
    error("must be run as root");
    exit EX_ERR;
}
unless (-d $confdir) {
    error("no such directory: $confdir");
    exit EX_ERR;
}

unless (-r "$confdir/snmpd.conf") {
    error("the file $confdir/snmpd.conf does not exist or is not readable",
	  "If NetSNMP configuration files are located in another directory,",
	  "then reconfigure mysqlstat with the --with-snmp-config-dir=DIR option",
	  "and rerun $progname");
    exit EX_ERR;
}
    
my @st = stat "$confdir/snmpd.conf"
    or die "can't stat $confdir/snmpd.conf: $!";
($owner_uid, $owner_gid) = @st[4..5];

# 1. Create MySQL user & grant it the necessary privs.
sql_create_user;
# 2. Edit $CONFDIR/mysqlstat.cnf
edit_cnf;    
# 3. Edit snmpd.conf
edit_snmp_conf;

Return to:

Send suggestions and report system problems to the System administrator.