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