IP addresses inherit tags from their parent networks (#375)
[racktables] / wwwroot / inc / triggers.php
1 <?php
2
3 # This file is a part of RackTables, a datacenter and server room management
4 # framework. See accompanying file "COPYING" for the full copyright and
5 # licensing information.
6
7 /*
8 *
9 * This file is a library of tab triggers for RackTables.
10 *
11 */
12
13 // Triggers may be optionally referred by some tabs of a page.
14 // In case they are defined, the given tab is only displayed if
15 // the trigger returned CSS class name. In certain cases, a key is necessary
16 // to decide (the 'bypass' hint of a page), and in some cases,
17 // other data can be used.
18
19 // APC "switched rack PDU" stands for a number of part numbers:
20 // http://www.apc.com/products/family/index.cfm?id=70
21 $known_APC_SKUs = array
22 (
23 // 100V input
24 1151 => 'AP7902J',
25 1152 => 'AP7930J',
26 1153 => 'AP7932J',
27 // 120V input
28 1154 => 'AP7900',
29 1155 => 'AP7901',
30 1156 => 'AP7902',
31 1157 => 'AP7930',
32 1158 => 'AP7931',
33 1159 => 'AP7932',
34 // 208V input
35 1160 => 'AP7911',
36 1161 => 'AP7940',
37 1162 => 'AP7941',
38 // 208V 3 phases input
39 1163 => 'AP7960',
40 1164 => 'AP7961',
41 1165 => 'AP7968',
42 1166 => 'AP7990',
43 1167 => 'AP7991',
44 1168 => 'AP7998',
45 // 230V input
46 1137 => 'AP7920',
47 1138 => 'AP7921',
48 1139 => 'AP7922',
49 1140 => 'AP7950',
50 1141 => 'AP7951',
51 1142 => 'AP7952',
52 1143 => 'AP7953',
53 1144 => 'AP7954',
54 // 400V 3 phases input
55 1154 => 'AP7957',
56 );
57
58 // Return 'std', if the object belongs to specified type and has
59 // specified attribute belonging to the given set of values.
60 function checkTypeAndAttribute ($object_id, $type_id, $attr_id, $values, $hit = 'std')
61 {
62 $object = spotEntity ('object', $object_id);
63 if ($object['objtype_id'] != $type_id)
64 return '';
65 foreach (getAttrValues ($object_id) as $record)
66 if ($record['id'] == $attr_id and in_array ($record['key'], $values))
67 return $hit;
68 return '';
69 }
70
71 // This trigger filters out everything except switches with known-good
72 // software.
73 function trigger_livevlans ()
74 {
75 return checkTypeAndAttribute
76 (
77 getBypassValue(),
78 8, // network switch
79 4, // SW type
80 // Cisco IOS 12.0
81 // Cisco IOS 12.1
82 // Cisco IOS 12.2
83 array (244, 251, 252)
84 );
85 }
86
87 // This trigger is on when any of the (get_mac_list, get_link_status) ops permitted
88 function trigger_liveports ()
89 {
90 $breed = detectDeviceBreed (getBypassValue());
91 foreach (array ('getportstatus', 'getmaclist') as $command)
92 if
93 (
94 validBreedFunction ($breed, $command) and
95 permitted (NULL, 'liveports', $command)
96 )
97 return 'std';
98 return '';
99 }
100
101 // SNMP port finder tab trigger. At the moment we decide on showing it
102 // for pristine switches/PDUs only. Once a user has begun
103 // filling the data in, we stop showing the tab.
104 function trigger_snmpportfinder ()
105 {
106
107 $object = spotEntity ('object', getBypassValue());
108 switch ($object['objtype_id'])
109 {
110 case 7: // any router
111 case 8: // or switch would suffice
112 return $object['nports'] ? '' : 'attn';
113 case 2: // but only selected PDUs
114 if ($object['nports'])
115 return '';
116 global $known_APC_SKUs;
117 return checkTypeAndAttribute
118 (
119 $object['id'],
120 2, // PDU
121 2, // HW type
122 array_keys ($known_APC_SKUs),
123 'attn'
124 );
125 default:
126 return '';
127 }
128 }
129
130 function trigger_isloadbalancer ()
131 {
132 return considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'IPV4LB_LISTSRC') ? 'std' : '';
133 }
134
135 function trigger_ip ()
136 {
137 if (count (getObjectIPAllocationList (getBypassValue())))
138 return 'std';
139 // Only hide the tab, if there are no addresses allocated.
140 return considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'IPV4OBJ_LISTSRC') ? 'std' : '';
141 }
142
143 function trigger_natv4 ()
144 {
145 if (!count (getObjectIPv4AllocationList (getBypassValue())))
146 return '';
147 return considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'IPV4NAT_LISTSRC') ? 'std' : '';
148 }
149
150 function trigger_autoports ()
151 {
152 $object = spotEntity ('object', getBypassValue());
153 amplifyCell ($object);
154 if (count ($object['ports']))
155 return '';
156 return count (getAutoPorts ($object['objtype_id'])) ? 'attn' : '';
157 }
158
159 function trigger_tags ()
160 {
161 global $taglist;
162 return count ($taglist) ? 'std' : '';
163 }
164
165 function trigger_passwdchange ()
166 {
167 global $user_auth_src;
168 return $user_auth_src == 'database' ? 'std' : '';
169 }
170
171 function trigger_localreports ()
172 {
173 global $localreports;
174 return count ($localreports) ? 'std' : '';
175 }
176
177 function trigger_file_editText ()
178 {
179 $fileInfo = spotEntity ('file', getBypassValue());
180 return ($fileInfo['type'] == 'text/plain') ? 'std' : '';
181 }
182
183 function trigger_rackspace ()
184 {
185 global $virtual_obj_types;
186
187 // Hide the tab if the object type is virtual
188 $object = spotEntity ('object', getBypassValue());
189 if (in_array($object['objtype_id'], $virtual_obj_types))
190 return '';
191
192 $rackspace = getRackspaceStats();
193 if ($rackspace['Racks'] > 0) return 'std';
194 return '';
195 }
196
197 function trigger_ports ()
198 {
199 // Hide the tab if the object type exists in the exclusion config option
200 if (considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'PORT_EXCLUSION_LISTSRC'))
201 return '';
202
203 return 'std';
204 }
205
206 // Offer the generic VLAN setup tab for every object, which already
207 // has a VLAN domain associated or at least can have one (in the latter
208 // case additionally heat the tab, if no domain is set.
209 function trigger_object_8021qorder ()
210 {
211 if (NULL !== getVLANSwitchInfo (getBypassValue()))
212 return 'std';
213 if (!count (getVLANDomainOptions()) or !count (getVSTOptions()))
214 return '';
215 if (considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'VLANSWITCH_LISTSRC'))
216 return 'attn';
217 return '';
218 }
219
220 function trigger_8021q_configured ()
221 {
222 if (!count (getVLANDomainOptions()) or !count (getVSTOptions()))
223 return '';
224 return 'std';
225 }
226
227 // implement similar logic for IPv4 networks
228 function trigger_ipv4net_vlanconfig ()
229 {
230 if (!count (getVLANDomainOptions())) // no domains -- no VLANs to bind with
231 return '';
232 $netinfo = spotEntity ('ipv4net', getBypassValue());
233 if ($netinfo['vlanc'])
234 return 'std';
235 elseif (considerConfiguredConstraint ($netinfo, 'VLANIPV4NET_LISTSRC'))
236 return 'attn';
237 else
238 return '';
239 }
240
241 // implement similar logic for IPv6 networks
242 function trigger_ipv6net_vlanconfig ()
243 {
244 if (!count (getVLANDomainOptions())) // no domains -- no VLANs to bind with
245 return '';
246 $netinfo = spotEntity ('ipv6net', getBypassValue());
247 if ($netinfo['vlanc'])
248 return 'std';
249 elseif (considerConfiguredConstraint ($netinfo, 'VLANIPV4NET_LISTSRC'))
250 return 'attn';
251 else
252 return '';
253 }
254
255 function trigger_vlan_ipv4net ()
256 {
257 $vlan_info = getVLANInfo (getBypassValue());
258 return count ($vlan_info['ipv4nets']) ? 'std' : 'attn';
259 }
260
261 function trigger_vlan_ipv6net ()
262 {
263 $vlan_info = getVLANInfo (getBypassValue());
264 return count ($vlan_info['ipv6nets']) ? 'std' : 'attn';
265 }
266
267 function trigger_object_8021qports ()
268 {
269 if (NULL === getVLANSwitchInfo (getBypassValue()))
270 return '';
271 return count (getStored8021QConfig (getBypassValue(), 'desired')) ? 'std' : '';
272 }
273
274 function trigger_object_8021qsync ()
275 {
276 if (NULL === $vswitch = getVLANSwitchInfo (getBypassValue()))
277 return '';
278 return $vswitch['out_of_sync'] == 'yes' ? 'attn' : 'std';
279 }
280
281 function trigger_LiveCDP ()
282 {
283 return trigger_anyDP ('getcdpstatus', 'CDP_RUNNERS_LISTSRC');
284 }
285
286 function trigger_LiveLLDP ()
287 {
288 return trigger_anyDP ('getlldpstatus', 'LLDP_RUNNERS_LISTSRC');
289 }
290
291 function trigger_anyDP ($command, $constraint)
292 {
293 if
294 (
295 validBreedFunction (detectDeviceBreed (getBypassValue()), $command) and
296 considerConfiguredConstraint (spotEntity ('object', getBypassValue()), $constraint)
297 )
298 return 'std';
299 return '';
300 }
301
302 // tease rules editor tab, when the VST has no rules
303 function trigger_vst_editrules()
304 {
305 $vst = spotEntity ('vst', getBypassValue());
306 return $vst['rulec'] ? 'std' : 'attn';
307 }
308
309 function triggerIPAddressLog ()
310 {
311 $ip_bin = assertIPArg ('ip');
312 switch (strlen ($ip_bin))
313 {
314 case 4:
315 $result = usePreparedSelectBlade ("SELECT COUNT(id) FROM IPv4Log WHERE ip = ?", array (ip4_bin2db ($ip_bin)));
316 break;
317 case 16:
318 $result = usePreparedSelectBlade ("SELECT COUNT(id) FROM IPv6Log WHERE ip = ?", array ($ip_bin));
319 break;
320 }
321 if ($row = $result->fetch(PDO::FETCH_NUM))
322 if ($row[0] > 0)
323 return 'std';
324 return '';
325 }
326
327 function triggerCactiGraphs ()
328 {
329 if (! count (getCactiServers()))
330 return '';
331 if
332 (
333 count (getCactiGraphsForObject (getBypassValue())) or
334 considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'CACTI_LISTSRC')
335 )
336 return 'std';
337 else
338 return '';
339 }
340
341 function trigger_ucs()
342 {
343 return checkTypeAndAttribute
344 (
345 getBypassValue(),
346 1787, # management interface
347 30, # mgmt type
348 array (1788) # UCS Manager
349 );
350 }
351
352 ?>