Debian Codegraph @

check_mysql_health/src/contrib/README.my-extensions

raw
1 # you will find instructions how to write extensions here
2
3 Self-written code is addressed by using a mode which starts with my-
4 --mode=my-thing-does ?
5
6 check_mysql_health will then look for a package named MyThing.
7
8 So you first have to write a Module which describes MyThing. Such a thing iinherits from DBD::MySQL::Server and needs two methods: init and nagios.
9
10 Start with a file called CheckMySQLHealthExt1.pm and skeleton code:
11
12 ###################################################
13 package MyThing;
14
15 our @ISA = qw(DBD::MySQL::Server);
16
17 sub init {
18 my $self = shift;
19 my %params = @_;
20 }
21
22 sub nagios {
23 my $self = shift;
24 my %params = @_;
25 }
26 ###################################################
27
28 When you call check_mysql_health with --mode=my-thing-does, it will
29 - create a DBD::MySQL::Server object
30 $obj = DBD::MySQL::Server->new()
31 - connect to the database
32 $obj->connect()
33 - re-bless the object
34 bless $obj, "MyThing"
35 - call $obj->init()
36 - if that was ok, call $obj->nagios()
37
38
39 So you need to write code which
40 - initializes the parameters you want to check
41 - calculates the nagios result from these parameters
42
43 For your convenience there are some predefined methods and variables:
44
45 Variable $self
46 $self is a hash-based object of type My::Thing
47 You can pass metrics from the init() method to the nagios() method by
48 adding attributes to the hash.
49 One important predefined attribute is $self->{handle} which points to
50 a database Connection object. You surely will use this.
51
52 Variable %params
53 $params{mode} contains the string you passed to the
54 --mode command line parameter, only with the "-" replaced by "::".
55 In the above example it will be "my::thing::does".
56 Because you can have only one init() method for your MyThing object but
57 more than one related modes (my-thing-does, my-thing-length, my-thing-rate)
58 you use $params{mode} for branching in your code. (see the example file)
59
60 Method add_nagios
61 $self->add_nagios(1, "i warn you");
62 This method can be called several times. The messages will be concatenated.
63 The first parameter is one of 0..3 and sets the nagios level. The worst level
64 among several calls to add_nagios will determine the plugin's exit code.
65
66 Method add_nagios_[ok|warning|critical|unknown]
67 $self->add_nagios_critical("i warned you!!! now it's too late");
68 $self->add_nagios_ok("everything is ok. i am the exit message");
69 These methods are wrappers for add_nagios which make your code more readable.
70
71 Method add_perfdata
72 $self->add_perfdata("metric1=0 metric2=100");
73 $self->add_perfdata("metric3=0);
74 $self->add_perfdata(sprintf "metric_xy=%d", $self->{xy});
75 You can call add_perfdata as often as you like.
76 The strings will be concatenated.
77
78 Method valdiff
79 $self->valdiff(\%params, qw(metric1 metric2));
80 Use this if you want to know how a metric has changed since the last run
81 of check_mysql_health.
82 Provided you have attributes metric1 and metric2 this method will create
83 new attributes for your $self object.
84 $self->{delta_metric1} which is the difference between the value of
85 $self->{metric1} during the current run and $self->{metric1} during the
86 last run.
87 $self->{delta_timestamp} which is the number of seconds which passed
88 since the last run of check_mysql_health.
89 If you have ever-growing values, you can simply calculate the rate:
90 $self->{metric1_per_second} = $self->{delta_metric1} / $self->{delta_timestamp}
91 The valdiff method will automatically save the current value to a state file
92 and read the past value from this file.
93 If you used the --name parameter which appears as $params{name} in your code
94 then you probably need to separate the saved values from each other. Otherwise
95 name1 would read the same saved value as name2. They would overwrite the
96 saved values. Use $params{differentiator} to use different state files.
97 $params{differenciator} = lc $self->{name};
98 $self->valdiff(\%params, qw(gets misses));
99
100 Method fetchrow_array
101 my($column1, $column2) = $self->{handle}->fetchrow_array(q{
102 SELECT col1, col2 FROM table1 where col1 = 'val1'
103 });
104 $self->{connected_users} = $self->{handle}->fetchrow_array(q{
105 SELECT COUNT(*) FROM v$session WHERE type = 'USER'
106 });
107 This method is used like the Perl DBI method fetchrow_array.
108
109
110 Method fetchall_array
111 my @results = $self->{handle}->fetchall_array(q{
112 SELECT col1, col2, col3 FROM table1
113 });
114 foreach (@results) {
115 my($column1, $column2, $column3) = @{$_};
116 ...
117 }
118 This method is used like the Perl DBI method fetchall_array.
119
120
121
122
123 Now you have written your first extension to check_mysql_health. Maybe you
124 just modified the example file contrib/CheckMySQLHealthExt1.pm
125 There are two methods how to import your own code into check_mysql_health:
126
127 - the static method
128 with ./configure --with-mymodules-dir=<dir> parameter you build a plugin which
129 contains both my code and your code in a single file. When you call "make"
130 every file in <dir> which matches CheckMySQLHealthExt*.pm is appended
131 to the final plugin check_mysql_health.
132
133 - the dynamic method
134 with ./configure --with-mymodules-dyn-dir=<dir> you build a plugin which will
135 search at runtime the <dir> for files matching CheckMySQLHealthExt*.pm and
136 import them. This way you can have different extensions on different hosts.
137 You also can modify your extension without having to rebuild the plugin.
138 On the other hand you have to distribute your extensions along with the plugin.
139