Modified: trunk/opsview-core/lib/Opsview/Utils/NDOLogsImporter.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/Utils/NDOLogsImporter.pm 2013-02-01 16:17:48 UTC (rev 11322)
+++ trunk/opsview-core/lib/Opsview/Utils/NDOLogsImporter.pm 2013-02-01 16:58:32 UTC (rev 11323)
@@ -1043,9 +1043,11 @@
$sth->execute();
}
+my $sth_SET_INACTIVE;
+
sub set_all_objects_as_inactive {
- my $sth = $DB->prepare_cached(
+ $sth_SET_INACTIVE = $DB->prepare_cached(
q{
UPDATE nagios_objects
SET
@@ -1053,13 +1055,15 @@
WHERE
instance_id = 1
}
- );
+ ) unless defined $sth_SET_INACTIVE;
- $sth->execute();
+ $sth_SET_INACTIVE->execute();
}
+my $sth_SET_ACTIVE;
+
sub set_object_as_active {
- my $sth = $DB->prepare_cached(
+ $sth_SET_ACTIVE = $DB->prepare_cached(
q{
UPDATE nagios_objects
SET
@@ -1071,9 +1075,10 @@
AND
object_id=?
}
- );
+ ) unless defined $sth_SET_ACTIVE;
- $sth->execute( $_[ ARG_OBJECT_TYPE() ], $_[ ARG_OBJECT_ID2() ] );
+ $sth_SET_ACTIVE->execute( $_[ ARG_OBJECT_TYPE() ], $_[ ARG_OBJECT_ID2() ]
+ );
}
sub get_latest_data_time {
@@ -1289,12 +1294,10 @@
$start_time_usec,
);
- my $rv = $sth_update_handle_NOTIFICATIONDATA->execute(@data);
- die "Update failed: ", $sth_update_handle_NOTIFICATIONDATA->errstr, "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_NOTIFICATIONDATA->execute(@data);
if ( $rv eq '0E0' ) {
- $sth_insert_handle_NOTIFICATIONDATA->execute(@data);
+ $sth_update_handle_NOTIFICATIONDATA->execute(@data);
# XXX handle_CONTACTNOTIFICATIONDATA is ignored so that's not needed
# if ( $event->[ NDO_DATA_TYPE() ] eq NEBTYPE_NOTIFICATION_START() ) {
@@ -1665,26 +1668,22 @@
$event->[ NDO_DATA_COMMENTID() ]
);
- my $rv = $sth_update_handle_COMMENTDATA->execute(@data);
- die "Update failed: ", $sth_update_handle_COMMENTDATA->errstr, "\n"
- unless $rv;
+ my $rv =
+ $sth_insert_handle_COMMENTDATA->execute( @data, $state_time,
+ $state_time_usec, );
if ( $rv eq '0E0' ) {
- $sth_insert_handle_COMMENTDATA->execute( @data, $state_time,
- $state_time_usec, );
+ $sth_update_handle_COMMENTDATA->execute(@data);
}
if ($is_current) {
my $rv =
- $sth_update_comments_handle_COMMENTDATA->execute(@data);
- die "Update failed: ",
- $sth_update_comments_handle_COMMENTDATA->errstr, "\n"
- unless $rv;
+ $sth_insert_comments_handle_COMMENTDATA->execute( @data,
+ $state_time, $state_time_usec, );
if ( $rv eq '0E0' ) {
- $sth_insert_comments_handle_COMMENTDATA->execute( @data,
- $state_time, $state_time_usec, );
+ $sth_update_comments_handle_COMMENTDATA->execute(@data);
}
}
@@ -1945,24 +1944,19 @@
$event->[ NDO_DATA_DOWNTIMEID() ]
);
- my $rv = $sth_update_handle_DOWNTIMEDATA->execute(@data);
- die "Update failed: ", $sth_update_handle_DOWNTIMEDATA->errstr, "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_DOWNTIMEDATA->execute(@data);
if ( $rv eq '0E0' ) {
- $sth_insert_handle_DOWNTIMEDATA->execute(@data);
+ $sth_update_handle_DOWNTIMEDATA->execute(@data);
}
if ($is_current) {
my $rv =
- $sth_update_schedule_handle_DOWNTIMEDATA->execute(@data);
- die "Update failed: ",
- $sth_update_schedule_handle_DOWNTIMEDATA->errstr, "\n"
- unless $rv;
+ $sth_insert_schedule_handle_DOWNTIMEDATA->execute(@data);
if ( $rv eq '0E0' ) {
- $sth_insert_schedule_handle_DOWNTIMEDATA->execute(@data);
+ $sth_update_schedule_handle_DOWNTIMEDATA->execute(@data);
}
}
@@ -2456,14 +2450,10 @@
# if the downtime is already set, then it will not get reset
if ($LOADING_RETENTION_DATA_FLAG) {
$rv =
- $sth_update_handle_HOSTSTATUSDATA->execute( @data, $object_id );
+ $sth_insert_handle_HOSTSTATUSDATA->execute( @data, $object_id );
- die "Update failed: ", $sth_update_handle_HOSTSTATUSDATA->errstr,
- "\n"
- unless $rv;
-
if ( $rv eq '0E0' ) {
- $sth_insert_handle_HOSTSTATUSDATA->execute( @data, $object_id );
+ $sth_update_handle_HOSTSTATUSDATA->execute( @data, $object_id );
}
}
else {
@@ -2803,29 +2793,21 @@
# if the downtime is already set, then it will not get reset
if ($LOADING_RETENTION_DATA_FLAG) {
$rv =
- $sth_update_handle_SERVICESTATUSDATA->execute( @data,
+ $sth_insert_handle_SERVICESTATUSDATA->execute( @data,
$object_id );
- die "Update failed: ",
- $sth_update_handle_SERVICESTATUSDATA->errstr, "\n"
- unless $rv;
-
if ( $rv eq '0E0' ) {
- $sth_insert_handle_SERVICESTATUSDATA->execute( @data,
+ $sth_update_handle_SERVICESTATUSDATA->execute( @data,
$object_id );
}
}
else {
$rv =
- $sth_update_downtime_handle_SERVICESTATUSDATA->execute( @data,
+ $sth_insert_downtime_handle_SERVICESTATUSDATA->execute( @data,
$event->[ NDO_DATA_SCHEDULEDDOWNTIMEDEPTH() ], $object_id );
- die "Update failed: ",
- $sth_update_downtime_handle_SERVICESTATUSDATA->errstr, "\n"
- unless $rv;
-
if ( $rv eq '0E0' ) {
- $sth_insert_downtime_handle_SERVICESTATUSDATA->execute( @data,
+ $sth_update_downtime_handle_SERVICESTATUSDATA->execute( @data,
$event->[ NDO_DATA_SCHEDULEDDOWNTIMEDEPTH() ], $object_id );
}
}
@@ -2850,18 +2832,6 @@
]
);
- my $sth_exists = $DB->prepare_cached(
- qq[
- SELECT COUNT(*) FROM nagios_] . $_[ ARG_PARENT() ] . q[group_members
- WHERE
- ] . $_[ ARG_PARENT() ] . q[group_id = ?
- AND
- ] . $_[ ARG_PARENT() ] . q[_object_id = ?
- AND
- instance_id = 1
- ]
- );
-
for my $var ( @{ $_[ ARG_MEMBERS() ] } ) {
next unless length $var;
@@ -2869,17 +2839,7 @@
my $member_id =
get_object_id_with_insert( $_[ ARG_GROUP_TYPE() ], $var );
- my $rv = $sth_exists->execute( $_[ ARG_OBJECT_ID() ], $member_id );
- die "Select failed: ", $sth_exists->errstr, "\n" unless $rv;
-
- my ($exists) = $sth_exists->fetchrow_array();
- $sth_exists->finish();
-
- unless ($exists) {
- my $rv = $sth_insert->execute( $_[ ARG_OBJECT_ID() ], $member_id );
- die "Insert failed: ", $sth_insert->errstr, "\n" unless $rv;
-
- }
+ $sth_insert->execute( $_[ ARG_OBJECT_ID() ], $member_id );
}
}
@@ -2894,18 +2854,6 @@
]
);
- my $sth_exists = $DB->prepare_cached(
- q[
- SELECT COUNT(*) FROM nagios_] . $_[ ARG_PARENT() ] . q[_contacts
- WHERE
- ] . $_[ ARG_PARENT() ] . q[_id = ?
- AND
- contact_object_id = ?
- AND
- instance_id = 1
- ]
- );
-
for my $var ( @{ $_[ ARG_CONTACTS() ] } ) {
next unless length $var;
@@ -2913,17 +2861,7 @@
my $member_id =
get_object_id_with_insert( NDO2DB_OBJECTTYPE_CONTACT(), $var );
- my $rv = $sth_exists->execute( $_[ ARG_OBJECT_ID3() ], $member_id );
- die "Select failed: ", $sth_exists->errstr, "\n" unless $rv;
-
- my ($exists) = $sth_exists->fetchrow_array();
- $sth_exists->finish();
-
- unless ($exists) {
- my $rv = $sth_insert->execute( $_[ ARG_OBJECT_ID3() ], $member_id );
- die "Insert failed: ", $sth_insert->errstr, "\n" unless $rv;
-
- }
+ $sth_insert->execute( $_[ ARG_OBJECT_ID3() ], $member_id );
}
}
@@ -2938,19 +2876,6 @@
]
);
- my $sth_exists = $DB->prepare_cached(
- q[
- SELECT COUNT(*) FROM nagios_]
- . $_[ ARG_PARENT() ] . q[_contactgroups
- WHERE
- ] . $_[ ARG_PARENT() ] . q[_id = ?
- AND
- contactgroup_object_id = ?
- AND
- instance_id = 1
- ]
- );
-
for my $var ( @{ $_[ ARG_CONTACTGROUPS() ] } ) {
next unless length $var;
@@ -2958,22 +2883,11 @@
my $member_id =
get_object_id_with_insert( NDO2DB_OBJECTTYPE_CONTACTGROUP(), $var );
- my $rv = $sth_exists->execute( $_[ ARG_OBJECT_ID3() ], $member_id );
- die "Select failed: ", $sth_exists->errstr, "\n" unless $rv;
-
- my ($exists) = $sth_exists->fetchrow_array();
- $sth_exists->finish();
-
- unless ($exists) {
- my $rv = $sth_insert->execute( $_[ ARG_OBJECT_ID3() ], $member_id );
- die "Insert failed: ", $sth_insert->errstr, "\n" unless $rv;
-
- }
+ $sth_insert->execute( $_[ ARG_OBJECT_ID3() ], $member_id );
}
}
my $sth_insert_handle_MULTI_PARENTHOST;
-my $sth_exists_handle_MULTI_PARENTHOST;
sub handle_MULTI_PARENTHOST {
@@ -2986,18 +2900,6 @@
}
) unless defined $sth_insert_handle_MULTI_PARENTHOST;
- $sth_exists_handle_MULTI_PARENTHOST = $DB->prepare_cached(
- q{
- SELECT COUNT(*) FROM nagios_host_parenthosts
- WHERE
- host_id = ?
- AND
- parent_host_object_id = ?
- AND
- instance_id = 1
- }
- ) unless defined $sth_exists_handle_MULTI_PARENTHOST;
-
for my $var ( @{ $_[ ARG_PARENTHOSTS() ] } ) {
next unless length $var;
@@ -3005,24 +2907,8 @@
my $member_id =
get_object_id_with_insert( NDO2DB_OBJECTTYPE_HOST(), $var );
- my $rv =
- $sth_exists_handle_MULTI_PARENTHOST->execute( $_[ ARG_OBJECT_ID2() ],
+ $sth_insert_handle_MULTI_PARENTHOST->execute( $_[ ARG_OBJECT_ID2() ],
$member_id );
- die "Select failed: ", $sth_exists_handle_MULTI_PARENTHOST->errstr, "\n"
- unless $rv;
-
- my ($exists) = $sth_exists_handle_MULTI_PARENTHOST->fetchrow_array();
- $sth_exists_handle_MULTI_PARENTHOST->finish();
-
- unless ($exists) {
- my $rv =
- $sth_insert_handle_MULTI_PARENTHOST->execute(
- $_[ ARG_OBJECT_ID2() ], $member_id );
- die "Insert failed: ", $sth_insert_handle_MULTI_PARENTHOST->errstr,
- "\n"
- unless $rv;
-
- }
}
}
@@ -3599,9 +3485,23 @@
my $sth_update_handle_HOSTDEFINITION;
my $sth_insert_handle_HOSTDEFINITION;
+my $sth_fetch_ID_handle_HOSTDEFINITION;
sub handle_HOSTDEFINITION {
+ $sth_fetch_ID_handle_HOSTDEFINITION = $DB->prepare_cached(
+ qq[
+ SELECT host_id
+ FROM nagios_hosts
+ WHERE
+ host_object_id=?
+ AND
+ config_type = ?
+ AND
+ instance_id = 1
+ ]
+ ) unless defined $sth_fetch_ID_handle_HOSTDEFINITION;
+
$sth_update_handle_HOSTDEFINITION = $DB->prepare_cached(
q{
UPDATE nagios_hosts SET
@@ -3837,16 +3737,20 @@
$CURRENT_OBJECT_CONFIG_TYPE,
);
- my $rv = $sth_update_handle_HOSTDEFINITION->execute(@data);
- die "Update failed: ", $sth_update_handle_HOSTDEFINITION->errstr, "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_HOSTDEFINITION->execute(@data);
if ( $rv eq '0E0' ) {
- my $rv = $sth_insert_handle_HOSTDEFINITION->execute(@data);
- die "Insert failed: ", $sth_insert_handle_HOSTDEFINITION->errstr,
- "\n"
- unless $rv;
+ $sth_update_handle_HOSTDEFINITION->execute(@data);
+ # get primary key for existing entry
+ $sth_fetch_ID_handle_HOSTDEFINITION->execute( $object_id,
+ $CURRENT_OBJECT_CONFIG_TYPE, );
+
+ ($host_id) = $sth_fetch_ID_handle_HOSTDEFINITION->fetchrow_array();
+ $sth_fetch_ID_handle_HOSTDEFINITION->finish();
+
+ }
+ else {
$host_id = $sth_insert_handle_HOSTDEFINITION->{mysql_insertid};
}
@@ -3855,7 +3759,6 @@
$object_id );
}
- # XXX on update there is no host_id - how to set parent relationship?
next unless $host_id;
if ( defined $event->[ NDO_DATA_PARENTHOST() ] ) {
@@ -3878,9 +3781,23 @@
my $sth_update_handle_SERVICEDEFINITION;
my $sth_insert_handle_SERVICEDEFINITION;
+my $sth_fetch_ID_handle_SERVICEDEFINITION;
sub handle_SERVICEDEFINITION {
+ $sth_fetch_ID_handle_SERVICEDEFINITION = $DB->prepare_cached(
+ qq[
+ SELECT service_id
+ FROM nagios_services
+ WHERE
+ service_object_id=?
+ AND
+ config_type = ?
+ AND
+ instance_id = 1
+ ]
+ ) unless defined $sth_fetch_ID_handle_SERVICEDEFINITION;
+
$sth_update_handle_SERVICEDEFINITION = $DB->prepare_cached(
q{
UPDATE nagios_services SET
@@ -4104,21 +4021,32 @@
$CURRENT_OBJECT_CONFIG_TYPE,
);
- my $rv = $sth_update_handle_SERVICEDEFINITION->execute(@data);
- die "Update failed: ", $sth_update_handle_SERVICEDEFINITION->errstr,
- "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_SERVICEDEFINITION->execute(@data);
if ( $rv eq '0E0' ) {
- my $rv = $sth_insert_handle_SERVICEDEFINITION->execute(@data);
- die "Insert failed: ",
- $sth_insert_handle_SERVICEDEFINITION->errstr, "\n"
- unless $rv;
+ $sth_update_handle_SERVICEDEFINITION->execute(@data);
+ # get primary key for existing entry
+ $sth_fetch_ID_handle_SERVICEDEFINITION->execute( $object_id,
+ $CURRENT_OBJECT_CONFIG_TYPE, );
+
+ ($service_id) =
+ $sth_fetch_ID_handle_SERVICEDEFINITION->fetchrow_array();
+ $sth_fetch_ID_handle_SERVICEDEFINITION->finish();
+
+ }
+ else {
$service_id =
$sth_insert_handle_SERVICEDEFINITION->{mysql_insertid};
}
+ if ( defined $event->[ NDO_DATA_CUSTOMVARIABLE() ] ) {
+ handle_MULTI_CUSTOMVARIABLE( $event->[ NDO_DATA_CUSTOMVARIABLE() ],
+ $object_id );
+ }
+
+ next unless $service_id;
+
if ( defined $event->[ NDO_DATA_CONTACTGROUP() ] ) {
handle_MULTI_CONTACTGROUP( 'service',
$event->[ NDO_DATA_CONTACTGROUP() ], $service_id );
@@ -4128,12 +4056,6 @@
handle_MULTI_CONTACT( 'service', $event->[ NDO_DATA_CONTACT() ],
$service_id );
}
-
- if ( defined $event->[ NDO_DATA_CUSTOMVARIABLE() ] ) {
- handle_MULTI_CUSTOMVARIABLE( $event->[ NDO_DATA_CUSTOMVARIABLE() ],
- $object_id );
- }
-
}
}
@@ -4224,18 +4146,10 @@
]
);
- my $rv = $sth_update_handle_SERVICEDEPENDENCYDEFINITION->execute(@data);
- die "Update failed: ",
- $sth_update_handle_SERVICEDEPENDENCYDEFINITION->errstr, "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_SERVICEDEPENDENCYDEFINITION->execute(@data);
if ( $rv eq '0E0' ) {
- my $rv =
- $sth_insert_handle_SERVICEDEPENDENCYDEFINITION->execute(@data);
- die "Insert failed: ",
- $sth_insert_handle_SERVICEDEPENDENCYDEFINITION->errstr, "\n"
- unless $rv;
-
+ $sth_update_handle_SERVICEDEPENDENCYDEFINITION->execute(@data);
}
}
}
@@ -4289,26 +4203,33 @@
$CURRENT_OBJECT_CONFIG_TYPE,
);
- my $rv = $sth_update_handle_COMMANDDEFINITION->execute(@data);
- die "Update failed: ", $sth_update_handle_COMMANDDEFINITION->errstr,
- "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_COMMANDDEFINITION->execute(@data);
if ( $rv eq '0E0' ) {
- my $rv = $sth_insert_handle_COMMANDDEFINITION->execute(@data);
- die "Insert failed: ",
- $sth_insert_handle_COMMANDDEFINITION->errstr, "\n"
- unless $rv;
-
+ $sth_update_handle_COMMANDDEFINITION->execute(@data);
}
}
}
my $sth_update_handle_TIMEPERIODDEFINITION;
my $sth_insert_handle_TIMEPERIODDEFINITION;
+my $sth_fetch_ID_handle_TIMEPERIODDEFINITION;
sub handle_TIMEPERIODDEFINITION {
+ $sth_fetch_ID_handle_TIMEPERIODDEFINITION = $DB->prepare_cached(
+ qq[
+ SELECT timeperiod_id
+ FROM nagios_timeperiods
+ WHERE
+ timeperiod_object_id=?
+ AND
+ config_type = ?
+ AND
+ instance_id = 1
+ ]
+ ) unless defined $sth_fetch_ID_handle_TIMEPERIODDEFINITION;
+
$sth_update_handle_TIMEPERIODDEFINITION = $DB->prepare_cached(
q{
UPDATE nagios_timeperiods SET
@@ -4355,21 +4276,27 @@
$CURRENT_OBJECT_CONFIG_TYPE,
);
- my $rv = $sth_update_handle_TIMEPERIODDEFINITION->execute(@data);
- die "Update failed: ", $sth_update_handle_TIMEPERIODDEFINITION->errstr,
- "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_TIMEPERIODDEFINITION->execute(@data);
if ( $rv eq '0E0' ) {
- my $rv = $sth_insert_handle_TIMEPERIODDEFINITION->execute(@data);
- die "Insert failed: ",
- $sth_insert_handle_TIMEPERIODDEFINITION->errstr, "\n"
- unless $rv;
+ $sth_update_handle_TIMEPERIODDEFINITION->execute(@data);
+ # get primary key for existing entry
+ $sth_fetch_ID_handle_TIMEPERIODDEFINITION->execute( $object_id,
+ $CURRENT_OBJECT_CONFIG_TYPE, );
+
+ ($timeperiod_id) =
+ $sth_fetch_ID_handle_TIMEPERIODDEFINITION->fetchrow_array();
+ $sth_fetch_ID_handle_TIMEPERIODDEFINITION->finish();
+
+ }
+ else {
$timeperiod_id =
$sth_insert_handle_TIMEPERIODDEFINITION->{mysql_insertid};
}
+ next unless $timeperiod_id;
+
if ( defined $event->[ NDO_DATA_TIMERANGE() ] ) {
handle_MULTI_TIMERANGE( $event->[ NDO_DATA_TIMERANGE() ],
$timeperiod_id );
@@ -4381,8 +4308,23 @@
my $sth_update_handle_CONTACTDEFINITION;
my $sth_insert_handle_CONTACTDEFINITION;
+#my $sth_fetch_ID_handle_CONTACTDEFINITION;
+
sub handle_CONTACTDEFINITION {
+ # $sth_fetch_ID_handle_CONTACTDEFINITION = $DB->prepare_cached(
+ # qq[
+ # SELECT contact_id
+ # FROM nagios_contacts
+ # WHERE
+ # contact_object_id=?
+ # AND
+ # config_type = ?
+ # AND
+ # instance_id = 1
+ # ]
+ # ) unless defined $sth_fetch_ID_handle_CONTACTDEFINITION;
+
$sth_update_handle_CONTACTDEFINITION = $DB->prepare_cached(
q{
UPDATE nagios_contacts SET
@@ -4503,19 +4445,24 @@
$CURRENT_OBJECT_CONFIG_TYPE,
);
- my $rv = $sth_update_handle_CONTACTDEFINITION->execute(@data);
- die "Update failed: ", $sth_update_handle_CONTACTDEFINITION->errstr,
- "\n"
- unless $rv;
+ my $rv = $sth_insert_handle_CONTACTDEFINITION->execute(@data);
if ( $rv eq '0E0' ) {
- my $rv = $sth_insert_handle_CONTACTDEFINITION->execute(@data);
- die "Insert failed: ",
- $sth_insert_handle_CONTACTDEFINITION->errstr, "\n"
- unless $rv;
+ $sth_update_handle_CONTACTDEFINITION->execute(@data);
- $contact_id =
- $sth_insert_handle_CONTACTDEFINITION->{mysql_insertid};
+ # get primary key for existing entry
+ # $sth_fetch_ID_handle_CONTACTDEFINITION->execute(
+ # $object_id,
+ # $CURRENT_OBJECT_CONFIG_TYPE,
+ # );
+ #
+ # ($contact_id) = $sth_fetch_ID_handle_CONTACTDEFINITION->fetchrow_array();
+ # $sth_fetch_ID_handle_CONTACTDEFINITION->finish();
+ #
+ #
+ # } else {
+ # $contact_id =
+ # $sth_insert_handle_CONTACTDEFINITION->{mysql_insertid};
}
# ignore NDO_DATA_CONTACTADDRESS