netdisco.pm - Utility functions used for back and front ends
This module provides utility functions for use with netdisco in both the front and backend. Front-end specific features are limited to the mason (.html) files, and the back-end specific features are limited to netdisco.
Max Baker
'Pg'
;
netdisco.conf
make_graph()
carp()
the SQL sent off to the server for Debugging.
If running under mason, the output of carp()
goes to the Apache
Error Log. From the shell it goes to STDERR.
Note that if you set this on a MASON page, the value will remain cached across most of the current httpd proccesses. Make sure you set it back to 0 via mason when you're done, unless you like watching Apache's error_log grow.
port_control_log
add_arp(mac,ip)
Expires old entries for given IP address.
Adds new entry or time stamps matching one.
add_node(mac,ip,port)
node
table.
Expires old entries matching given arguments.
Adds a new entry or time stamps matching old entry.
add_nbt(ip,mac,nbname,domain,server,nbuser)
Expires old entries for given MAC address.
Adds new entry or time stamps matching one.
bits_to_mask(bits)
config()
%CONFIG
hash.
has_layer(00000100,3) = true
hostname(ip)
getip(host)
in_subnet(subnet,ip)
in_subnet('192.168.0.0/24','192.168.0.3') = 1; in_subnet('192.168.0.3','192.168.0.3') = 1;
in_subnets(ip,config_directive)
print in_subnets('192.168.0.1','macsuck_no');
Returns reference to hash. Keys are IP addresses in dotted decimal that are in the subnet.
Gateway and Broadcast (.0 .255) addresses are not included.
%$hash_ref = dump_subnet('192.168.0.0/24'); scalar keys %$hash_ref == 254;
Also accepted :
dump_subnet('14.0/16'); dump_subnet('4/24');
is_mac(mac)
Checks for types :
08002b:010203 08002b-010203 0800.2b01.0203 08-00-2b-01-02-03 08:00:2b:01:02:03
log(class,text)
log
table.
log('error',"this is an error");
mail(to,subject,body)
mask_to_bits(mask)
mask_to_bits('255.255.0.0') = 16
Returns true if the server want's to be secure and is, or true if the server doesn't want to be secure.
Returns false if the server is not secure but wants to be.
url_secure(url)
sort_ip()
sort {}
calls to sort by IP octet.
If passed two hashes, will sort on the key ip
or remote_ip
.
sort_port()
sort()
- Sort port names with the following formatting types :
A5 5 FastEthernet0/1 FastEthernet0/1-atm 5.5 Port:3
Works on hashes if a key named port exists.
Cheers to Bradley Baetz (bbaetz) for improvements in this sub.
make_graph()
Graph::Undirected
object that represents the discovered
network.
Graph is made by loading all the device_port
entries
that have a neighbor, using them as edges. Then each device seen in those
entries is added as a vertex.
Nodes without topology information are not included.
root_device(ip)
If the given IP Address matches an alias of a device, returns the IP of the device the alias belongs to.
user_add(user,%args)
%args
can have key values of { pw, admin, port }
Returns error message if problem.
user_del(user)
Returns result from DBI->do()
Integer for number of rows deleted, or undef if error.
dbh()
Select database handle in use by localizing $netdisco::DB
dbh_quote($text)
quote()
on the text and returns it.
hash_diff($hashref_orig,$hashref_new)
\%matching
and either
updates or inserts into table with \%values
accodringly.
insert_or_update('device', { 'ip'=>'127.0.0.1' }, { 'ip' => '127.0.0.1', dns => 'dog' } );
First time called it will insert the new entry
Second time called it will modify the entry with the values.
Supports
* Auto Quoting of Values
Returns undef if problem.
On inserts in PostgreSQL, returns the OID of the row inserted.
Or returns value from DBD::St::execute()
sql_column(table,[key_col,val_col],{where})
$hash{key_val}={val_val}
If multiple matches are found for key_col, only the last one is kept.
Supports
* (NOT) NULL * Auto-Quoting Values
$OldDevices = sql_column('device',['ip','layers']);
Creates the hash %$OldDevices where the key is the IP address and the Value is the Layers
sql_do(sql)
$dbh->do()
No quoting.
Internally calls sql_rows()
-- See for Usage.
my $hashref = sql_hash('device',['ip','ports'], {'ip'=>'127.0.0.1'});
sql_match(text,exact_flag)
Optional exact_flag specifies whether or not to search for that exact text search or to do a *text*.
Default is non_exact.
$dbh->fetchrow_hashref
Supports
* Joins * Pattern Matching * NULL/NOT NULL * Boolean OR or AND criteria * Auto-Quotes all values and Override * IN (list) and NOT IN (list) clauses
Pass a true value for the OR argument to join constraints in the WHERE clause by OR instead of AND.
$matches = sql_rows('device',['ip','dns','uptime]);
sql_rows('device d, device e',['d.ip,d.dns,e.ip,e.dns'], {'d.dns' => \'e.dns'});
Creates the SQL:
SELECT d.ip,d.dns,e.ip,e.dns FROM device d, device e WHERE d.dns = e.dns;
This also leaves a security hole if the where value is coming from the outside
world because someone could stuff in 'dog'
;delete from node where true;...>
as a value. If you turn off quoting make sure the program is feeding the where
value.
sql_rows('device',['*'], {'age(creation)' => \\"< interval '1 day'"})
Creates the sql:
SELECT * FROM device WHERE age(creation) < interval '1 day';
$matches = sql_rows('device',['ip','name'],{'dns'=>'IS NULL'});
$matches = sql_rows('device d left join device_ip i on d.ip = i.ip', ['distinct(d.ip)','d.dns','i.alias','i.ip'], {'d.ip/i.alias'=>'127.0.0.1', 'd.dns/i.dns' => 'dog%'}, 1);
Selects all rows within device
and device_ip
where
device_ip.alias
or device.ip
are localhost
or device_ip.dns
or device.dns
has dog
in it.
Where columns with slashes are split into separate search terms combined with OR
:
{ 'd.ip/i.alias' => '127.0.0.1' }
Creates the SQL
"WHERE (d.ip = '127.0.0.1' OR i.alias = '127.0.0.1')"
$matches = sql_rows('device',['ip'],{'dns' => ['cat%','-g%'] },1 );
Creates the SQL:
SELECT ip FROM device WHERE (dns like 'cat%') OR (dns like '-g%');
Single array reference is for creating multiple WHERE
entries (see above)
$matches = sql_rows('node',['mac'], {'substr(mac,1,8)' => [['00:00:00','00:00:0c']]})
Creates the SQL:
SELECT mac FROM node WHERE (substr(mac,1,8) IN ('00:00:00','00:00:0c'));
!
$matches = sql_rows('device',['name'], {'vendor' => [['!cisco','hp']] });
Will find all devices that are neither cisco or hp.
$matches = sql_rows('device',['ip','dns'], undef, undef, 'order by dns limit 10');
sql_scalar(table,[column],{where})
Internally calls sql_hash()
which calls sql_rows()
All arguments are passed directly to sql_hash()
my $count_ip = sql_scalar('device',['COUNT(ip)'],{'name' => 'dog'});
sql_vacuum(table,%opts)
Pass the table name as '' if you want to vacuum all tables.
Options:
verbose => 1 (set if DEBUG) full => 1