Skip to content

Commit b32072c

Browse files
committed
change metrics to per_minute and adapt tests
1 parent d2136e2 commit b32072c

8 files changed

Lines changed: 162 additions & 40 deletions

src/centreon/plugins/values.pm

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ sub new {
5454
$self->{threshold_crit} = undef;
5555

5656
$self->{per_second} = 0;
57+
$self->{per_minute} = 0;
5758
$self->{manual_keys} = 0;
5859
$self->{last_timestamp} = undef;
5960

@@ -96,14 +97,15 @@ sub calc {
9697

9798
# manage only one value ;)
9899
foreach my $value (@{$self->{key_values}}) {
99-
if (defined($value->{diff}) && $value->{diff} == 1) {
100-
$self->{result_values}->{$value->{name}} = $options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}};
101-
} elsif (defined($value->{per_second}) && $value->{per_second} == 1) {
102-
$self->{result_values}->{$value->{name}} = ($options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}}) / $options{delta_time};
103-
} elsif (defined($value->{per_minute}) && $value->{per_minute} == 1) {
104-
$self->{result_values}->{$value->{name}} = ($options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}}) / ($options{delta_time} / 60);
100+
my $instance_name = $self->{instance} . '_' . $value->{name};
101+
if ($value->{diff}) {
102+
$self->{result_values}->{$value->{name}} = $options{new_datas}->{$instance_name} - $options{old_datas}->{$instance_name};
103+
} elsif ($value->{per_second}) {
104+
$self->{result_values}->{$value->{name}} = ($options{new_datas}->{$instance_name} - $options{old_datas}->{$instance_name}) / $options{delta_time};
105+
} elsif ($value->{per_minute}) {
106+
$self->{result_values}->{$value->{name}} = ($options{new_datas}->{$instance_name} - $options{old_datas}->{$instance_name}) / ($options{delta_time} / 60);
105107
} else {
106-
$self->{result_values}->{$value->{name}} = $options{new_datas}->{$self->{instance} . '_' . $value->{name}};
108+
$self->{result_values}->{$value->{name}} = $options{new_datas}->{$instance_name};
107109
}
108110
}
109111

src/database/mssql/mode/deadlocks.pm

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use warnings;
2525
use base qw(centreon::plugins::templates::counter);
2626
use centreon::plugins::constants qw(:counters);
2727
use centreon::plugins::misc qw(is_excluded);
28+
use Digest::SHA qw(sha256_hex);
2829

2930
sub prefix_by_instance_output {
3031
my ($self, %options) = @_;
@@ -41,9 +42,9 @@ sub set_counters {
4142
];
4243

4344
$self->{maps_counters}->{total} = [
44-
{ label => 'deadlocks', type => COUNTER_KIND_METRIC, nlabel => 'total#mssql.deadlocks.count', set => {
45-
key_values => [ { name => 'total' } ],
46-
output_template => '%.2f total dead locks/s',
45+
{ label => 'deadlocks', type => COUNTER_KIND_METRIC, nlabel => 'total#mssql.deadlocks.perminute', set => {
46+
key_values => [ { name => 'total', per_minute => 1 } ],
47+
output_template => '%.2f total dead locks/min',
4748
perfdatas => [
4849
{ template => '%.2f', min => 0 },
4950
],
@@ -52,9 +53,9 @@ sub set_counters {
5253
];
5354

5455
$self->{maps_counters}->{by_instance} = [
55-
{ label => 'deadlocks-by-instance', nlabel => 'mssql.deadlocks.count', set => {
56-
key_values => [ { name => 'value' }, { name => 'display' } ],
57-
output_template => '%.2f dead locks/s',
56+
{ label => 'deadlocks-by-instance', nlabel => 'mssql.deadlocks.perminute', set => {
57+
key_values => [ { name => 'value', per_minute => 1 }, { name => 'display' } ],
58+
output_template => '%.2f dead locks/min',
5859
perfdatas => [
5960
{ template => '%.2f', min => 0, label_extra_instance => 1, instance_use => 'display' },
6061
],
@@ -63,10 +64,9 @@ sub set_counters {
6364
];
6465
}
6566

66-
6767
sub new {
6868
my ($class, %options) = @_;
69-
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
69+
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
7070
bless $self, $class;
7171

7272
$options{options}->add_options(arguments => {
@@ -96,7 +96,6 @@ sub manage_selection {
9696
my $query_result = $options{sql}->fetchall_arrayref();
9797
$self->{total}->{total} = 0;
9898

99-
10099
foreach my $row (@{$query_result}) {
101100
my ($instance, $value) = @$row;
102101
next if $instance eq '_Total';
@@ -109,6 +108,10 @@ sub manage_selection {
109108
}
110109
$self->{output}->option_exit(short_msg => "No locks counter found with given filters")
111110
unless $self->{by_instance} && keys %{$self->{by_instance}};
111+
112+
$self->{cache_name} = 'mssql_' . $self->{mode} . '_' . $options{sql}->get_unique_id4save() . '_' .
113+
sha256_hex($self->{option_results}->{include_instance}) . '_' .
114+
sha256_hex($self->{option_results}->{exclude_instance});
112115
}
113116

114117
1;
@@ -117,7 +120,7 @@ __END__
117120
118121
=head1 MODE
119122
120-
Check MSSQL dead locks per second
123+
Check MSSQL dead locks per minute
121124
122125
=over 8
123126

tests/database/mssql/dead-locks-mock-data.pl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020

2121
[
2222
[ '_Total', 317 ],
23-
[ 'Xact', 0 ],
24-
[ 'RowGroup', 0 ],
25-
[ 'OIB', 0 ],
26-
[ 'AllocUnit', 0 ],
27-
[ 'HoBT', 0 ],
23+
[ 'Xact', 18 ],
24+
[ 'RowGroup', 5 ],
25+
[ 'OIB', 56 ],
26+
[ 'AllocUnit', 19 ],
27+
[ 'HoBT', 39 ],
2828
[ 'Metadata', 38 ],
29-
[ 'Application', 0 ],
29+
[ 'Application', 512 ],
3030
[ 'RID', 24 ],
31-
[ 'Extent', 0 ],
31+
[ 'Extent', 7 ],
3232
[ 'Key', 101 ],
33-
[ 'Page', 0 ],
34-
[ 'Object', 2 ],
35-
[ 'File', 141 ],
36-
[ 'Database', 11 ],
33+
[ 'Page', 41 ],
34+
[ 'Object', 54 ],
35+
[ 'File', 562 ],
36+
[ 'Database', 45 ],
3737
]

tests/database/mssql/dead-locks.robot

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*** Settings ***
2-
Documentation database::mssql::plugin
2+
Documentation In this robot file, before each test case, we restore the statefile to an initial state and age, and
3+
... we fix the time so that the age of the data is always 1 minute. Every value, calculated as a per_minute
4+
... average is consequently predictible.
35
46
Resource ${CURDIR}${/}..${/}..${/}resources/import.resource
57

@@ -9,7 +11,7 @@ Test Timeout 120s
911

1012

1113
*** Variables ***
12-
${INJECT_PERL} -MDBD::Sybase -I${CURDIR}
14+
${INJECT_PERL} -MDBD::Sybase -Mfixed_date -I${CURDIR}
1315
${CMD} ${CENTREON_PLUGINS}
1416
... --plugin=database::mssql::plugin
1517
... --mode=dead-locks
@@ -22,6 +24,7 @@ ${CMD} ${CENTREON_PLUGINS}
2224
*** Test Cases ***
2325
Dead-locks ${tc}
2426
[Tags] database mssql
27+
[Setup] Ctn Dead Locks Test Setup
2528

2629
${OLD_PERL5OPT}= Get Environment Variable PERL5OPT default=
2730
Set Environment Variable PERL5OPT ${INJECT_PERL} ${OLD_PERL5OPT}
@@ -40,28 +43,36 @@ Dead-locks ${tc}
4043
... --
4144
... 1
4245
... ${EMPTY}
43-
... OK: 317.00 total dead locks/s | 'total#mssql.deadlocks.count'=317.00;;;0; 'AllocUnit#mssql.deadlocks.count'=0.00;;;0; 'Application#mssql.deadlocks.count'=0.00;;;0; 'Database#mssql.deadlocks.count'=11.00;;;0; 'Extent#mssql.deadlocks.count'=0.00;;;0; 'File#mssql.deadlocks.count'=141.00;;;0; 'HoBT#mssql.deadlocks.count'=0.00;;;0; 'Key#mssql.deadlocks.count'=101.00;;;0; 'Metadata#mssql.deadlocks.count'=38.00;;;0; 'OIB#mssql.deadlocks.count'=0.00;;;0; 'Object#mssql.deadlocks.count'=2.00;;;0; 'Page#mssql.deadlocks.count'=0.00;;;0; 'RID#mssql.deadlocks.count'=24.00;;;0; 'RowGroup#mssql.deadlocks.count'=0.00;;;0; 'Xact#mssql.deadlocks.count'=0.00;;;0;
46+
... OK: 0.00 total dead locks/min | 'total#mssql.deadlocks.perminute'=0.00;;;0; 'AllocUnit#mssql.deadlocks.perminute'=8.00;;;0; 'Application#mssql.deadlocks.perminute'=1.00;;;0; 'Database#mssql.deadlocks.perminute'=5.00;;;0; 'Extent#mssql.deadlocks.perminute'=3.00;;;0; 'File#mssql.deadlocks.perminute'=30.00;;;0; 'HoBT#mssql.deadlocks.perminute'=2.00;;;0; 'Key#mssql.deadlocks.perminute'=51.00;;;0; 'Metadata#mssql.deadlocks.perminute'=7.00;;;0; 'OIB#mssql.deadlocks.perminute'=4.00;;;0; 'Object#mssql.deadlocks.perminute'=1.00;;;0; 'Page#mssql.deadlocks.perminute'=1.00;;;0; 'RID#mssql.deadlocks.perminute'=4.00;;;0; 'RowGroup#mssql.deadlocks.perminute'=4.00;;;0; 'Xact#mssql.deadlocks.perminute'=3.00;;;0;
4447
... 2
4548
... --filter-database=Database
46-
... OK: 11.00 total dead locks/s - 11.00 dead locks/s | 'total#mssql.deadlocks.count'=11.00;;;0; 'Database#mssql.deadlocks.count'=11.00;;;0;
49+
... OK: 45.00 total dead locks/min - 5.00 dead locks/min | 'total#mssql.deadlocks.perminute'=45.00;;;0; 'Database#mssql.deadlocks.perminute'=5.00;;;0;
4750
... 3
4851
... --include-instance=Database
49-
... OK: 11.00 total dead locks/s - 11.00 dead locks/s | 'total#mssql.deadlocks.count'=11.00;;;0; 'Database#mssql.deadlocks.count'=11.00;;;0;
52+
... OK: 45.00 total dead locks/min - 5.00 dead locks/min | 'total#mssql.deadlocks.perminute'=45.00;;;0; 'Database#mssql.deadlocks.perminute'=5.00;;;0;
5053
... 4
5154
... --exclude-instance=Database
52-
... OK: 306.00 total dead locks/s | 'total#mssql.deadlocks.count'=306.00;;;0; 'AllocUnit#mssql.deadlocks.count'=0.00;;;0; 'Application#mssql.deadlocks.count'=0.00;;;0; 'Extent#mssql.deadlocks.count'=0.00;;;0; 'File#mssql.deadlocks.count'=141.00;;;0; 'HoBT#mssql.deadlocks.count'=0.00;;;0; 'Key#mssql.deadlocks.count'=101.00;;;0; 'Metadata#mssql.deadlocks.count'=38.00;;;0; 'OIB#mssql.deadlocks.count'=0.00;;;0; 'Object#mssql.deadlocks.count'=2.00;;;0; 'Page#mssql.deadlocks.count'=0.00;;;0; 'RID#mssql.deadlocks.count'=24.00;;;0; 'RowGroup#mssql.deadlocks.count'=0.00;;;0; 'Xact#mssql.deadlocks.count'=0.00;;;0;
55+
... OK: 1476.00 total dead locks/min | 'total#mssql.deadlocks.perminute'=1476.00;;;0; 'AllocUnit#mssql.deadlocks.perminute'=8.00;;;0; 'Application#mssql.deadlocks.perminute'=1.00;;;0; 'Extent#mssql.deadlocks.perminute'=3.00;;;0; 'File#mssql.deadlocks.perminute'=30.00;;;0; 'HoBT#mssql.deadlocks.perminute'=2.00;;;0; 'Key#mssql.deadlocks.perminute'=51.00;;;0; 'Metadata#mssql.deadlocks.perminute'=7.00;;;0; 'OIB#mssql.deadlocks.perminute'=4.00;;;0; 'Object#mssql.deadlocks.perminute'=1.00;;;0; 'Page#mssql.deadlocks.perminute'=1.00;;;0; 'RID#mssql.deadlocks.perminute'=4.00;;;0; 'RowGroup#mssql.deadlocks.perminute'=4.00;;;0; 'Xact#mssql.deadlocks.perminute'=3.00;;;0;
5356
... 5
5457
... --include-instance=Database --warning-deadlocks-by-instance=1
55-
... WARNING: 11.00 dead locks/s | 'total#mssql.deadlocks.count'=11.00;;;0; 'Database#mssql.deadlocks.count'=11.00;0:1;;0;
58+
... WARNING: 5.00 dead locks/min | 'total#mssql.deadlocks.perminute'=45.00;;;0; 'Database#mssql.deadlocks.perminute'=5.00;0:1;;0;
5659
... 6
5760
... --include-instance=Database --critical-deadlocks-by-instance=1
58-
... CRITICAL: 11.00 dead locks/s | 'total#mssql.deadlocks.count'=11.00;;;0; 'Database#mssql.deadlocks.count'=11.00;;0:1;0;
61+
... CRITICAL: 5.00 dead locks/min | 'total#mssql.deadlocks.perminute'=45.00;;;0; 'Database#mssql.deadlocks.perminute'=5.00;;0:1;0;
5962
... 7
6063
... --include-instance=unexisting
6164
... UNKNOWN: No locks counter found with given filters
6265
... 8
63-
... --include-instance=Database --warning-deadlocks=1
64-
... WARNING: 11.00 total dead locks/s | 'total#mssql.deadlocks.count'=11.00;0:1;;0; 'Database#mssql.deadlocks.count'=11.00;;;0;
66+
... --include-instance=Database --warning-deadlocks=40
67+
... WARNING: 45.00 total dead locks/min | 'total#mssql.deadlocks.perminute'=45.00;0:40;;0; 'Database#mssql.deadlocks.perminute'=5.00;;;0;
6568
... 9
66-
... --include-instance=Database --critical-deadlocks=1
67-
... CRITICAL: 11.00 total dead locks/s | 'total#mssql.deadlocks.count'=11.00;;0:1;0; 'Database#mssql.deadlocks.count'=11.00;;;0;
69+
... --include-instance=Database --critical-deadlocks=40
70+
... CRITICAL: 45.00 total dead locks/min | 'total#mssql.deadlocks.perminute'=45.00;;0:40;0; 'Database#mssql.deadlocks.perminute'=5.00;;;0;
71+
72+
73+
*** Keywords ***
74+
Ctn Dead Locks Test Setup
75+
@{statefiles}= List Files In Directory ${CURDIR}${/}statefiles mssql_dead-locks_*
76+
FOR ${statefile} IN @{statefiles}
77+
Copy File ${CURDIR}${/}statefiles${/}${statefile} ${/}var${/}lib${/}centreon${/}centplugins${/}
78+
END

tests/database/mssql/fixed_date.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fixed_date;
2+
3+
# Copyright 2025-Present Centreon
4+
# Always use the same fixed date to test certificate validity
5+
6+
BEGIN {
7+
*CORE::GLOBAL::time = sub { 1781177760 };
8+
}
9+
10+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"AllocUnit_value": 11,
3+
"Application_display": "Application",
4+
"RowGroup_display": "RowGroup",
5+
"Application_value": 511,
6+
"Database_display": "Database",
7+
"Page_value": 40,
8+
"Extent_value": 4,
9+
"HoBT_value": 37,
10+
"Object_value": 53,
11+
"OIB_display": "OIB",
12+
"File_value": 532,
13+
"Key_value": 50,
14+
"Metadata_value": 31,
15+
"Xact_display": "Xact",
16+
"RID_display": "RID",
17+
"RID_value": 20,
18+
"Key_display": "Key",
19+
"HoBT_display": "HoBT",
20+
"Metadata_display": "Metadata",
21+
"total_total": 1521,
22+
"Extent_display": "Extent",
23+
"OIB_value": 52,
24+
"Page_display": "Page",
25+
"Object_display": "Object",
26+
"RowGroup_value": 1,
27+
"last_timestamp": 1781177700,
28+
"AllocUnit_display": "AllocUnit",
29+
"Xact_value": 15,
30+
"File_display": "File",
31+
"Database_value": 40
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"AllocUnit_value": 11,
3+
"Application_display": "Application",
4+
"RowGroup_display": "RowGroup",
5+
"Application_value": 511,
6+
"Database_display": "Database",
7+
"Page_value": 40,
8+
"Extent_value": 4,
9+
"HoBT_value": 37,
10+
"Object_value": 53,
11+
"OIB_display": "OIB",
12+
"File_value": 532,
13+
"Key_value": 50,
14+
"Metadata_value": 31,
15+
"Xact_display": "Xact",
16+
"RID_display": "RID",
17+
"RID_value": 20,
18+
"Key_display": "Key",
19+
"HoBT_display": "HoBT",
20+
"Metadata_display": "Metadata",
21+
"total_total": 1521,
22+
"Extent_display": "Extent",
23+
"OIB_value": 52,
24+
"Page_display": "Page",
25+
"Object_display": "Object",
26+
"RowGroup_value": 1,
27+
"last_timestamp": 1781177700,
28+
"AllocUnit_display": "AllocUnit",
29+
"Xact_value": 15,
30+
"File_display": "File",
31+
"Database_value": 40
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"AllocUnit_value": 11,
3+
"Application_display": "Application",
4+
"RowGroup_display": "RowGroup",
5+
"Application_value": 511,
6+
"Database_display": "Database",
7+
"Page_value": 40,
8+
"Extent_value": 4,
9+
"HoBT_value": 37,
10+
"Object_value": 53,
11+
"OIB_display": "OIB",
12+
"File_value": 532,
13+
"Key_value": 50,
14+
"Metadata_value": 31,
15+
"Xact_display": "Xact",
16+
"RID_display": "RID",
17+
"RID_value": 20,
18+
"Key_display": "Key",
19+
"HoBT_display": "HoBT",
20+
"Metadata_display": "Metadata",
21+
"total_total": 1521,
22+
"Extent_display": "Extent",
23+
"OIB_value": 52,
24+
"Page_display": "Page",
25+
"Object_display": "Object",
26+
"RowGroup_value": 1,
27+
"last_timestamp": 1781177700,
28+
"AllocUnit_display": "AllocUnit",
29+
"Xact_value": 15,
30+
"File_display": "File",
31+
"Database_value": 40
32+
}

0 commit comments

Comments
 (0)