-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset-hostattr
executable file
·151 lines (120 loc) · 4.42 KB
/
set-hostattr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to set host attributes
#
use strict;
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_v $opt_u $opt_f);
getopts ('hdvuf');
my $debug = defined ($opt_d);
my $verbose = defined ($opt_v);
my $update = defined ($opt_u);
my $force = defined ($opt_f);
$verbose = 1 if ($debug);
if ($#ARGV != 4 || $opt_h) {
die (<<EOH);
Syntax: $0 [-dvuf] <ID/IP/FQDN/MAC> attribute section type value
Options :
-d debug
-v verbose
-u update lastupdated timestamp if same value
-f force, overwrite old value if present
attribute is the name
section is the section
type is string, integer or blob
value is what you want it set to
EOH
}
my $searchfor = shift;
my $attribute = shift;
my $section = shift;
my $type = shift;
my $value = shift;
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my @host_refs = $hostdb->findhost ('guess', $searchfor);
if ($hostdb->{error}) {
die ("$0: $hostdb->{error}\n");
}
if (@host_refs) {
if (scalar @host_refs != 1) {
my @hl;
foreach my $host (@host_refs) {
my $id = $host->id ();
my $ip = $host->ip () || 'no ip';
my $hn = $host->hostname () || 'no hostname';
my $mac = $host->mac_address () || 'no mac address';
push (@hl, "id $id, $ip, $hn, $mac");
}
die ("$0: Host match pattern '$searchfor' is ambigous. Hosts matching :\n " . join ("\n ", @hl) . "\n");
}
my $host = shift @host_refs;
my $res = do_host ($hostdb, $host, $attribute, $section, $type, $value, $debug, $verbose);
exit (1) if (! defined ($res)) or (! $res);
}
exit (0);
sub do_host
{
my $hostdb = shift;
my $host = shift;
my $attribute = shift;
my $section = shift;
my $type = shift;
my $value = shift;
my $debug = shift;
my $verbose = shift;
my $idstr = get_hostid ($host);
$host->init_attributes ();
my $attr = $host->get_attribute ($attribute, $section);
my $old_value = 'none';
$old_value = $attr->get () if (defined ($attr));
print ("$0: $idstr: Found attribute $attr (value '$old_value')\n") if ($debug);
if (defined ($attr)) {
my $old_type = $attr->type ();
if ($old_value eq $value and $old_type eq $type) {
if ($update) {
print ("$idstr: Updating lastupdated timestamp since value remains the same.\n") if ($verbose);
$attr->lastupdated ('NOW') or warn ("$0: $idstr: Could not update lastupdated timestamp - $attr->{error}\n"), return undef;
$attr->commit () or warn ("$0: $idstr: Could not commit host attribute object - $attr->{error}\n"), return undef;
} else {
print ("$idstr: Value remains the same. Not updating lastupdated timestamp since -u was not supplied.\n") if ($verbose);
return 0;
}
} else {
if ($force) {
print ("$idstr: Changing value for existing attribute from '$old_value' (type '$old_type') to '$value' (type '$type').\n") if ($verbose);
$attr->set ($type, $value) or warn ("$0: $idstr: Could not set value '$value' of type '$type' - $attr->{error}\n"), return undef;
$attr->lastupdated ('NOW') or warn ("$0: $idstr: Could not update lastupdated timestamp - $attr->{error}\n"), return undef;
$attr->lastmodified ('NOW') or warn ("$0: $idstr: Could not update lastmodified timestamp - $attr->{error}\n"), return undef;
$attr->commit () or warn ("$0: $idstr: Could not commit host attribute object - $attr->{error}\n"), return undef;
} else {
warn ("$idstr: NOT changing value (from '$old_value' to '$value') for existing attribute since -f was not supplied.\n");
return 0;
}
}
} else {
print ("$idstr: Creating new attribute of type '$type'.\n") if ($verbose);
# new attribute
$attr = $host->create_hostattribute ();
$attr->key ($attribute);
$attr->section ($section);
$attr->set ($type, $value) or warn ("$0: $idstr: Could not set value '$value' of type '$type' - $attr->{error}\n"), return undef;
$attr->lastupdated ('NOW') or warn ("$0: $idstr: Could not update lastupdated timestamp - $attr->{error}\n"), return undef;
$attr->lastmodified ('NOW') or warn ("$0: $idstr: Could not update lastmodified timestamp - $attr->{error}\n"), return undef;
$attr->commit () or warn ("$0: $idstr: Could not commit host attribute object - $attr->{error}\n"), return undef;
}
return 1;
}
sub get_hostid
{
my $host = shift;
my $id = $host->id ();
my $ip = $host->ip () || 'no ip';
my $hn = $host->hostname () || 'no hostname';
my $mac = $host->mac_address () || 'no mac address';
return ("id $id, $ip, $hn, $mac");
}