centstorage2broker_identifyMetric-2.3.8.patch

Quentin Garnier, 05/28/2012 11:43 am

Download (5.07 KB)

identifyMetric.pm 2012-05-28 11:33:55.000000000 +0200
50 50
}
51 51

  
52 52
sub removeSpecialCharInMetric($){
53
    $_[0] =~ s/\./\-/g;
54
    $_[0] =~ s/\,/\-/g;
55
    $_[0] =~ s/\:/\-/g;
56
    $_[0] =~ s/\ /\-/g;
57
    return $_[0];
53
    my $new_metric_name = $_[0];
54

  
55
    $new_metric_name =~ s/\./\-/g;
56
    $new_metric_name =~ s/\,/\-/g;
57
    $new_metric_name =~ s/\:/\-/g;
58
    $new_metric_name =~ s/\ /\-/g;
59
    return $new_metric_name;
58 60
}
59 61

  
60 62
sub insertMetrics($$$$$$$){
......
147 149
	
148 150
		if (defined($metric_name) && $metric_name && defined($value)) {
149 151
		    # Check if metric is known...
150
		    $data[0] = removeSpecialCharInMetric($data[0]);
152
		    #$data[0] = removeSpecialCharInMetric($data[0]);
151 153
	
152
		    my $sth1 = $con_ods->prepare("SELECT * FROM `metrics` WHERE `index_id` = '".$_[1]."' AND `metric_name` = ".$con_ods->quote($data[0]));
154
		    my $sth1 = $con_ods->prepare("SELECT * FROM `metrics` WHERE `index_id` = '".$_[1]."' AND `metric_name` = ".$con_ods->quote($metric_name));
153 155
		    if (!$sth1->execute()) {
154 156
				writeLogFile("Error:" . $sth1->errstr . "\n");
155 157
		    } else {
156 158
				if ($sth1->rows() eq 0) {
157
				    $just_insert = 1;  
158
				    insertMetrics($_[1], $data[0], $data[2], $data[3], $data[4], $data[5], $data[6]);
159
		
160
				    # Get ID
161
				    $sth1 = $con_ods->prepare("SELECT * FROM `metrics` WHERE `index_id` = '".$_[1]."' AND `metric_name` = ".$con_ods->quote($data[0]));
162
				    if (!$sth1->execute()) {
159
					my $metric_name_mod = removeSpecialCharInMetric($metric_name);
160
					$sth1 = $con_ods->prepare("SELECT * FROM `metrics` WHERE `index_id` = '".$_[1]."' AND `metric_name` = ". $con_ods->quote($metric_name_mod));
161
					if (!$sth1->execute()) {
163 162
						writeLogFile("Error:" . $sth1->errstr . "\n");
164
				    }
163
				        } else {
164
						if ($sth1->rows() eq 0) {
165
							$just_insert = 1; 
166
							insertMetrics($_[1], $metric_name, $data[2], $data[3], $data[4], $data[5], $data[6]);
167
							# Get ID
168
							$sth1 = $con_ods->prepare("SELECT * FROM `metrics` WHERE `index_id` = '".$_[1]."' AND `metric_name` = " . $con_ods->quote($metric_name));
169
							if (!$sth1->execute) {
170
								writeLogFile("Error:" . $sth1->errstr . "\n");
171
							}
172
						} else {
173
							# Update with good metric name
174
							my $sth2 = $con_ods->prepare("UPDATE `metrics` SET `metric_name` = " . $con_ods->quote($metric_name) . " WHERE `index_id` = '".$_[1]."' AND `metric_name` = " . $con_ods->quote($metric_name_mod));
175
							if (!$sth2->execute) {
176
								writeLogFile("Error:" . $sth1->errstr . "\n");
177
							}
178
						}
179
					}
165 180
				}
166 181
				my $metric = $sth1->fetchrow_hashref();
167 182
				$sth1->finish();
168 183
	
169 184
				# Update metric attributs
170 185
				if ($just_insert || (defined($data[2]) && defined($metric->{'unit_name'}) && $metric->{'unit_name'} ne $data[2])) {
171
				    my $sth1 = $con_ods->prepare("UPDATE `metrics` SET `unit_name` = '".$data[2]."', `warn` = '".$data[3]."', `crit` = '".$data[4]."', `min` = '".$data[5]."', `max` = '".$data[6]."' WHERE `metric_id` = '".$metric->{'metric_id'}."'");
186
				    $sth1 = $con_ods->prepare("UPDATE `metrics` SET `unit_name` = '".$data[2]."', `warn` = '".$data[3]."', `crit` = '".$data[4]."', `min` = '".$data[5]."', `max` = '".$data[6]."' WHERE `metric_id` = '".$metric->{'metric_id'}."'");
172 187
				    if (!$sth1->execute()) {
173 188
						writeLogFile("Error:" . $sth1->errstr . "\n");
174 189
				    }
......
183 198
				    # manage 'data_source_type' default value : NULL = '0'
184 199
		            $metric->{'data_source_type'} = defined($metric->{'data_source_type'}) ? $metric->{'data_source_type'} : 0;
185 200
				    if (defined($_[4]) && $_[4] eq 0 && $_[6] eq 0){
186
						updateRRDDB($configuration->{'RRDdatabase_path'}, $metric->{'metric_id'}, $_[3], $data[1], ($_[3] - 200), $configuration->{'len_storage_rrd'}, $metric->{'metric_name'}, $metric->{'data_source_type'});
201
						updateRRDDB($configuration->{'RRDdatabase_path'}, $metric->{'metric_id'}, $_[3], $data[1], ($_[3] - 200), $configuration->{'len_storage_rrd'}, $metric_name, $metric->{'data_source_type'});
187 202
				    } elsif (defined($_[4]) && $_[4] eq 2) { 
188
						updateRRDDB($configuration->{'RRDdatabase_path'}, $metric->{'metric_id'}, $_[3], $data[1], ($_[3] - 200), $configuration->{'len_storage_rrd'}, $metric->{'metric_name'}, $metric->{'data_source_type'});
203
						updateRRDDB($configuration->{'RRDdatabase_path'}, $metric->{'metric_id'}, $_[3], $data[1], ($_[3] - 200), $configuration->{'len_storage_rrd'}, $metric_name, $metric->{'data_source_type'});
189 204
						updateMysqlDB($metric->{'metric_id'}, $_[3], $data[1], $status{$_[2]});
190 205
				    }
191 206
				}	    
......
260 275
	    undef($sth1);
261 276

  
262 277
	    if ($just_insert || (defined($data[2]) && defined($metric->{'unit_name'}) && $metric->{'unit_name'} ne $data[2])) {
263
		my $sth1 = $con_ods->prepare("UPDATE `metrics` SET `unit_name` = '".$data[2]."' WHERE `metric_id` = '".$metric->{'metric_id'}."'");
278
		$sth1 = $con_ods->prepare("UPDATE `metrics` SET `unit_name` = '".$data[2]."' WHERE `metric_id` = '".$metric->{'metric_id'}."'");
264 279
		if (!$sth1->execute){
265 280
		    writeLogFile("Error:" . $sth1->errstr . "\n");
266 281
		}