use
overload
'""'
=>
sub
{
shift
->ip },
fallback
=> 1;
__PACKAGE__->table(
"device"
);
__PACKAGE__->add_columns(
"ip"
,
{
data_type
=>
"inet"
,
is_nullable
=> 0 },
"creation"
,
{
data_type
=>
"timestamp"
,
default_value
=> \
"LOCALTIMESTAMP"
,
is_nullable
=> 1,
original
=> {
default_value
=> \
"LOCALTIMESTAMP"
},
},
"dns"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"description"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"uptime"
,
{
data_type
=>
"bigint"
,
is_nullable
=> 1 },
"contact"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"name"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"location"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"layers"
,
{
data_type
=>
"varchar"
,
is_nullable
=> 1,
size
=> 8 },
"num_ports"
,
{
data_type
=>
"integer"
,
is_serializable
=> 0,
is_nullable
=> 1 },
"mac"
,
{
data_type
=>
"macaddr"
,
is_nullable
=> 1 },
"serial"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"chassis_id"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"model"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"ps1_type"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"ps2_type"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"ps1_status"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"ps2_status"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"fan"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"slots"
,
{
data_type
=>
"integer"
,
is_nullable
=> 1 },
"vendor"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"os"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"os_ver"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"log"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"snmp_ver"
,
{
data_type
=>
"integer"
,
is_nullable
=> 1 },
"snmp_comm"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"snmp_class"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"snmp_engineid"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"vtp_domain"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"vtp_mode"
,
{
data_type
=>
"text"
,
is_nullable
=> 1 },
"last_discover"
,
{
data_type
=>
"timestamp"
,
is_nullable
=> 1 },
"last_macsuck"
,
{
data_type
=>
"timestamp"
,
is_nullable
=> 1 },
"last_arpnip"
,
{
data_type
=>
"timestamp"
,
is_nullable
=> 1 },
"is_pseudo"
,
{
data_type
=>
"boolean"
,
is_nullable
=> 0,
default_value
=> \
"false"
},
"pae_is_enabled"
,
{
data_type
=>
"boolean"
,
is_nullable
=> 1 },
"custom_fields"
,
{
data_type
=>
"jsonb"
,
is_nullable
=> 0,
default_value
=> \
"{}"
},
"tags"
,
{
data_type
=>
"text[]"
,
is_nullable
=> 0,
default_value
=> \
"'{}'::text[]"
},
);
__PACKAGE__->set_primary_key(
"ip"
);
__PACKAGE__->has_many(
device_ips
=>
'App::Netdisco::DB::Result::DeviceIp'
,
'ip'
);
__PACKAGE__->has_many(
device_ips_by_address_or_name
=>
'App::Netdisco::DB::Result::DeviceIp'
,
sub
{
my
$args
=
shift
;
return
{
"$args->{foreign_alias}.ip"
=> {
-ident
=>
"$args->{self_alias}.ip"
},
-or
=> [
"$args->{foreign_alias}.dns"
=> {
'ilike'
, \
'?'
},
"$args->{foreign_alias}.alias"
=> {
'<<='
, \
'?'
},
"$args->{foreign_alias}.alias::text"
=> {
'ilike'
, \
'?'
},
],
};
},
{
cascade_copy
=> 0,
cascade_update
=> 0,
cascade_delete
=> 0 }
);
__PACKAGE__->has_many(
vlans
=>
'App::Netdisco::DB::Result::DeviceVlan'
,
'ip'
);
__PACKAGE__->has_many(
ports
=>
'App::Netdisco::DB::Result::DevicePort'
,
'ip'
);
__PACKAGE__->has_many(
ports_by_mac
=>
'App::Netdisco::DB::Result::DevicePort'
,
sub
{
my
$args
=
shift
;
return
{
"$args->{foreign_alias}.ip"
=> {
-ident
=>
"$args->{self_alias}.ip"
},
"$args->{foreign_alias}.mac::text"
=> {
'ilike'
, \
'?'
},
};
},
{
cascade_copy
=> 0,
cascade_update
=> 0,
cascade_delete
=> 0 }
);
__PACKAGE__->has_many(
modules
=>
'App::Netdisco::DB::Result::DeviceModule'
,
'ip'
);
__PACKAGE__->has_many(
power_modules
=>
'App::Netdisco::DB::Result::DevicePower'
,
'ip'
);
__PACKAGE__->has_many(
oids
=>
'App::Netdisco::DB::Result::DeviceBrowser'
,
'ip'
);
__PACKAGE__->has_many(
port_vlans
=>
'App::Netdisco::DB::Result::DevicePortVlan'
,
'ip'
, {
join_type
=>
'RIGHT'
}
);
__PACKAGE__->has_many(
port_vlans_filter
=>
'App::Netdisco::DB::Result::DevicePortVlan'
,
'ip'
, {
join_type
=>
'INNER'
}
);
sub
vlan {
return
(
shift
)->vlans->first }
__PACKAGE__->has_many(
wireless_ports
=>
'App::Netdisco::DB::Result::DevicePortWireless'
,
'ip'
, {
join_type
=>
'RIGHT'
}
);
__PACKAGE__->has_many(
ssids
=>
'App::Netdisco::DB::Result::DevicePortSsid'
,
'ip'
, {
join_type
=>
'RIGHT'
}
);
__PACKAGE__->has_many(
properties_ports
=>
'App::Netdisco::DB::Result::DevicePortProperties'
,
'ip'
, {
join_type
=>
'RIGHT'
}
);
__PACKAGE__->has_many(
powered_ports
=>
'App::Netdisco::DB::Result::DevicePortPower'
,
'ip'
, {
join_type
=>
'RIGHT'
}
);
__PACKAGE__->might_have(
community
=>
'App::Netdisco::DB::Result::Community'
,
'ip'
);
__PACKAGE__->might_have(
snapshot
=>
'App::Netdisco::DB::Result::DeviceSnapshot'
,
'ip'
);
__PACKAGE__->has_one(
throughput
=>
'App::Netdisco::DB::Result::Virtual::DevicePortSpeed'
,
'ip'
);
sub
has_layer {
my
(
$device
,
$layer
) =
@_
;
return
unless
$layer
and
$layer
=~ m/^[1-7]$/;
return
(
$device
->layers and (
substr
(
$device
->layers, (8-
$layer
), 1) == 1));
}
sub
renumber {
my
(
$device
,
$ip
) =
@_
;
my
$schema
=
$device
->result_source->schema;
my
$new_addr
= NetAddr::IP::Lite->new(
$ip
)
or
return
;
my
$old_ip
=
$device
->ip;
my
$new_ip
=
$new_addr
->addr;
return
if
$new_ip
eq
'0.0.0.0'
;
$schema
->resultset(
'DeviceIp'
)
->search({
ip
=>
$old_ip
,
alias
=>
$old_ip
})
->update({
ip
=>
$new_ip
,
alias
=>
$new_ip
});
$schema
->resultset(
'DeviceIp'
)
->search({
ip
=>
$old_ip
,
alias
=>
$new_ip
})->
delete
();
foreach
my
$set
(
qw/
DeviceBrowser
DeviceIp
DeviceModule
DevicePower
DeviceSnapshot
DeviceVlan
DevicePort
DevicePortLog
DevicePortPower
DevicePortProperties
DevicePortSsid
DevicePortVlan
DevicePortWireless
/
) {
$schema
->resultset(
$set
)
->search({
ip
=>
$old_ip
})
->update({
ip
=>
$new_ip
});
}
$schema
->resultset(
'DeviceSkip'
)
->search({
device
=>
$new_ip
})->
delete
;
$schema
->resultset(
'DeviceSkip'
)
->search({
device
=>
$old_ip
})
->update({
device
=>
$new_ip
});
$schema
->resultset(
'DevicePort'
)
->search({
remote_ip
=>
$old_ip
})
->update({
remote_ip
=>
$new_ip
});
$schema
->resultset(
'Node'
)
->search({
switch
=>
$old_ip
})
->update({
switch
=>
$new_ip
});
$schema
->storage->dbh_do(
sub
{
my
(
$storage
,
$dbh
,
@extra
) =
@_
;
local
$dbh
->{TraceLevel} = (
$ENV
{DBIC_TRACE} ?
'1|SQL'
:
$dbh
->{TraceLevel});
my
$router_first_sql
=
q{
UPDATE node_ip
SET seen_on_router_first = seen_on_router_first - ?::text || jsonb_build_object(?::text, seen_on_router_first -> ?::text)
WHERE seen_on_router_first \? ?::text
}
;
$dbh
->
do
(
$router_first_sql
,
undef
,
$old_ip
,
$new_ip
,
$old_ip
,
$old_ip
);
my
$router_last_sql
=
q{
UPDATE node_ip
SET seen_on_router_last = seen_on_router_last - ?::text || jsonb_build_object(?::text, seen_on_router_last -> ?::text)
WHERE seen_on_router_last \? ?::text
}
;
$dbh
->
do
(
$router_last_sql
,
undef
,
$old_ip
,
$new_ip
,
$old_ip
,
$old_ip
);
});
$schema
->resultset(
'Topology'
)
->search({
dev1
=>
$old_ip
})
->update({
dev1
=>
$new_ip
});
$schema
->resultset(
'Topology'
)
->search({
dev2
=>
$old_ip
})
->update({
dev2
=>
$new_ip
});
$schema
->resultset(
'Admin'
)->search({
device
=>
$old_ip
,
})->
delete
;
$device
->update({
ip
=>
$new_ip
,
dns
=> (hostname_from_ip(
$new_ip
)
||
eval
{
$schema
->resultset(
'DeviceIp'
)->find(
$new_ip
,
$new_ip
)->dns } ||
undef
),
});
return
$device
;
}
sub
port_count {
return
(
shift
)->get_column(
'port_count'
) }
sub
uptime_age {
return
(
shift
)->get_column(
'uptime_age'
) }
sub
first_seen_stamp {
return
(
shift
)->get_column(
'first_seen_stamp'
) }
sub
last_discover_stamp {
return
(
shift
)->get_column(
'last_discover_stamp'
) }
sub
last_macsuck_stamp {
return
(
shift
)->get_column(
'last_macsuck_stamp'
) }
sub
last_arpnip_stamp {
return
(
shift
)->get_column(
'last_arpnip_stamp'
) }
sub
since_last_discover {
return
(
shift
)->get_column(
'since_last_discover'
) }
sub
since_last_macsuck {
return
(
shift
)->get_column(
'since_last_macsuck'
) }
sub
since_last_arpnip {
return
(
shift
)->get_column(
'since_last_arpnip'
) }
1;