Coverage for src/zenossapi/routers/template.py: 90%

413 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-25 05:47 +0000

1# -*- coding: utf-8 -*- 

2 

3""" 

4Zenoss template_router 

5""" 

6 

7from zenossapi.apiclient import ZenossAPIClientError 

8from zenossapi.routers import ZenossRouter 

9 

10 

11class TemplateRouter(ZenossRouter): 

12 """ 

13 Class for interacting with the Zenoss template router 

14 """ 

15 

16 def __init__(self, url, headers, ssl_verify): 

17 super(TemplateRouter, self).__init__(url, headers, ssl_verify, 'template_router', 'TemplateRouter') 

18 self.uid = None 

19 self.properties = None 

20 

21 def __repr__(self): 

22 if self.uid: 

23 identifier = self.uid 

24 else: 

25 identifier = hex(id(self)) 

26 

27 return '<{0} object at {1}>'.format( 

28 type(self).__name__, identifier 

29 ) 

30 

31 def _get_properties(self, zobject): 

32 """ 

33 Gets the properties of an object. 

34 

35 Arguments: 

36 zobject (str): The uid of the Zenoss object (device, component, 

37 etc.) to get the properties of 

38 

39 Returns: 

40 dict: 

41 """ 

42 return self._router_request( 

43 self._make_request_data( 

44 'getInfo', 

45 dict(uid=zobject) 

46 ) 

47 ) 

48 

49 def _get_template_by_uid(self, template_uid): 

50 """ 

51 Gets a template by its full UID 

52 

53 Arguments: 

54 template_uid (str): UID of the template 

55 

56 Returns: 

57 ZenossTemplate: 

58 """ 

59 template_data = self._router_request( 

60 self._make_request_data( 

61 'getInfo', 

62 dict(uid=template_uid) 

63 ) 

64 ) 

65 return ZenossTemplate( 

66 self.api_url, 

67 self.api_headers, 

68 self.ssl_verify, 

69 template_data['data'] 

70 ) 

71 

72 def _get_data_source_by_uid(self, datasource_uid): 

73 """ 

74 Get a data source by its full UID. 

75 

76 Arguments: 

77 datasource_uid (str): UID of the data source to get 

78 

79 Returns: 

80 ZenossDataSource: 

81 """ 

82 data_source_data = self._router_request( 

83 self._make_request_data( 

84 'getDataSourceDetails', 

85 dict(uid=datasource_uid), 

86 ) 

87 ) 

88 return ZenossDataSource( 

89 self.api_url, 

90 self.api_headers, 

91 self.ssl_verify, 

92 data_source_data['record'] 

93 ) 

94 

95 def _get_data_point_by_uid(self, datapoint_uid): 

96 """ 

97 Get a data point by its full UID. 

98 

99 Arguments: 

100 datapoint_uid (str): UID of the data point to get details for 

101 

102 Returns: 

103 ZenossDataPoint: 

104 """ 

105 dp_data = self._router_request( 

106 self._make_request_data( 

107 'getDataPointDetails', 

108 dict(uid=datapoint_uid), 

109 ) 

110 ) 

111 return ZenossDataPoint( 

112 self.api_url, 

113 self.api_headers, 

114 self.ssl_verify, 

115 dp_data['record'] 

116 ) 

117 

118 def _get_threshold_by_uid(self, threshold_uid): 

119 """ 

120 Gets a threshold by its full UID 

121 

122 Arguments: 

123 threshold_uid (str): UID of the template 

124 

125 Returns: 

126 ZenossThreshold: 

127 """ 

128 threshold_data = self._router_request( 

129 self._make_request_data( 

130 'getThresholdDetails', 

131 dict(uid=threshold_uid) 

132 ) 

133 ) 

134 

135 return ZenossThreshold( 

136 self.api_url, 

137 self.api_headers, 

138 self.ssl_verify, 

139 threshold_data['record'] 

140 ) 

141 

142 def _get_graph_by_uid(self, graph_uid): 

143 """ 

144 Get a graph by its full UID. 

145 

146 Arguments: 

147 graph_uid (str): UID of the graph to get the definition of 

148 

149 Returns: 

150 ZenossGraph: 

151 """ 

152 graph_data = self._router_request( 

153 self._make_request_data( 

154 'getGraphDefinition', 

155 dict(uid=graph_uid), 

156 ) 

157 ) 

158 

159 return ZenossGraph( 

160 self.api_url, 

161 self.api_headers, 

162 self.ssl_verify, 

163 graph_data['data'] 

164 ) 

165 

166 def _find_templates_in_tree(self, templates_data): 

167 """ 

168 Works through the dict structure returned by the Zenoss API for 

169 template queries and returns the defined templates from it. 

170 

171 Arguments: 

172 templates_data (dict): Templates data returned by the API 

173 

174 Returns: 

175 list: 

176 """ 

177 templates = [] 

178 for node in templates_data['children']: 

179 if node['leaf']: 

180 if node['text'].find("Locally Defined") > -1: 

181 templates.append((node['uid'].replace('/zport/dmd/', '', 1), node['qtip'])) 

182 else: 

183 templates.extend(self._find_templates_in_tree(node)) 

184 

185 return templates 

186 

187 def _set_properties(self, properties): 

188 """ 

189 Sets arbitrary properties of any object. 

190 

191 Arguments: 

192 properties (dict): Properties and values to set 

193 

194 Returns: 

195 dict: 

196 """ 

197 return self._router_request( 

198 self._make_request_data( 

199 'setInfo', 

200 properties 

201 ) 

202 ) 

203 

204 def set_properties(self, properties): 

205 """ 

206 Sets properties of an object. 

207 

208 Arguments: 

209 properties (dict): Properties and values to set 

210 """ 

211 if not isinstance(properties, dict): 

212 raise ZenossAPIClientError('Type error: Properties to set for {} must be a dict'.format(type(self).__name__)) 

213 

214 if not self.uid: 

215 return 

216 

217 data = properties 

218 data['uid'] = self.uid 

219 properties_result = self._set_properties(data) 

220 for prop in properties: 

221 if getattr(self, prop, False): 

222 setattr(self, prop, properties[prop]) 

223 elif getattr(self, 'properties', False) and prop in self.properties: 

224 self.properties[prop] = properties[prop] 

225 

226 return properties_result 

227 

228 def get_all_templates(self): 

229 """ 

230 Returns all defined templates. 

231 

232 Returns: 

233 list(ZenossTemplate): 

234 """ 

235 templates_data = self._router_request( 

236 self.get_device_class_templates( 

237 device_class='Devices' 

238 ) 

239 ) 

240 

241 templates = [] 

242 found_templates = self._find_templates_in_tree(templates_data[0]) 

243 for t in found_templates: 

244 templates.append( 

245 self._get_template_by_uid(t[0]) 

246 ) 

247 return templates 

248 

249 def list_all_templates(self): 

250 """ 

251 Returns all defined templates as a list of tuples containing the 

252 template UID and description. 

253 

254 Returns: 

255 list(ZenossTemplate): 

256 """ 

257 templates_data = self._router_request( 

258 self.get_device_class_templates( 

259 device_class='Devices' 

260 ) 

261 ) 

262 

263 templates = [] 

264 found_templates = self._find_templates_in_tree(templates_data[0]) 

265 for t in found_templates: 

266 templates.append(t) 

267 return templates 

268 

269 def get_device_class_templates(self, device_class): 

270 """ 

271 Gets the defined templates for a device class 

272 

273 Arguments: 

274 device_class (str): Device class to get templates for 

275 

276 Returns: 

277 list(ZenossTemplate): 

278 """ 

279 templates_data = self._router_request( 

280 self._make_request_data( 

281 'getDeviceClassTemplates', 

282 dict(id=device_class), 

283 ) 

284 ) 

285 

286 templates = [] 

287 found_templates = self._find_templates_in_tree(templates_data[0]) 

288 for t in found_templates: 

289 templates.append( 

290 self._get_template_by_uid(t[0]) 

291 ) 

292 return templates 

293 

294 def list_device_class_templates(self, device_class): 

295 """ 

296 Returns the defined templates for a device class as a list of 

297 tuples containing the template UID and description. 

298 

299 Arguments: 

300 device_class (str): Device class to list templates for 

301 

302 Returns: 

303 list(str): 

304 """ 

305 if not device_class.startswith('Devices'): 

306 if device_class.startswith('/'): 

307 device_class = 'Devices{0}'.format(device_class) 

308 else: 

309 device_class = 'Devices/{0}'.format(device_class) 

310 templates_data = self._router_request( 

311 self._make_request_data( 

312 'getDeviceClassTemplates', 

313 dict(id=device_class), 

314 ) 

315 ) 

316 

317 templates = [] 

318 found_templates = self._find_templates_in_tree(templates_data[0]) 

319 for t in found_templates: 

320 templates.append(t) 

321 return templates 

322 

323 def get_object_templates(self, zenoss_object): 

324 """ 

325 Gets the templates bound to a specific object 

326 (monitored resource or component) 

327 

328 Arguments: 

329 zenoss_object (str): The uid of the object, e.g. 

330 Devices/Server/Zuora/Aspose/devices/10.aspose.prod.slv.zuora 

331 

332 Returns: 

333 list(ZenossTemplate): 

334 """ 

335 if not zenoss_object.startswith('Devices'): 

336 if zenoss_object.startswith('/'): 

337 zenoss_object = 'Devices{0}'.format(zenoss_object) 

338 else: 

339 zenoss_object = 'Devices/{0}'.format(zenoss_object) 

340 

341 templates_data = self._router_request( 

342 self._make_request_data( 

343 'getObjTemplates', 

344 dict(uid=zenoss_object), 

345 ) 

346 ) 

347 

348 templates = [] 

349 found_templates = templates_data['data'] 

350 for t in found_templates: 

351 templates.append( 

352 self._get_template_by_uid(t['uid'].replace('/zport/dmd/', '', 1)) 

353 ) 

354 return templates 

355 

356 def get_template(self, device_class, template): 

357 """ 

358 Get a Zenoss template 

359 

360 Arguments: 

361 device_class (str): Name of the device class where the template is defined 

362 template (str): Name of the template to get 

363 

364 Returns: 

365 ZenossTemplate: 

366 """ 

367 # Check to see if this is a local template instead of a device class 

368 # template 

369 if "devices" in device_class: 

370 template_uid = '{0}/{1}'.format(device_class, template) 

371 else: 

372 template_uid = '{0}/rrdTemplates/{1}'.format(device_class, template) 

373 

374 return self._get_template_by_uid(template_uid) 

375 

376 def add_template(self, target, name): 

377 """ 

378 Adds a template to a device class. 

379 

380 Arguments: 

381 target (str): The uid of the target device class 

382 name (str): Unique name of the template to add 

383 

384 Returns: 

385 ZenossTemplate: 

386 """ 

387 if not target.startswith('Devices'): 

388 if target.startswith('/'): 

389 target = 'Devices{0}'.format(target) 

390 else: 

391 target = 'Devices/{0}'.format(target) 

392 

393 if not target.endswith('rrdTemplates'): 

394 target = target + '/rrdTemplates' 

395 

396 template_data = self._router_request( 

397 self._make_request_data( 

398 'addTemplate', 

399 dict( 

400 id=name, 

401 targetUid=target, 

402 ) 

403 ) 

404 ) 

405 

406 return self._get_template_by_uid(template_data['nodeConfig']['uid'].replace('/zport/dmd/', '', 1)) 

407 

408 def delete_template(self, device_class, template): 

409 """ 

410 Removes a template. 

411 

412 Arguments: 

413 device_class (str): Name of the device class where the template is defined 

414 template (str): Name of the template to remove 

415 

416 Returns: 

417 dict: 

418 """ 

419 if not device_class.startswith('Devices'): 

420 if device_class.startswith('/'): 

421 device_class = 'Devices{0}'.format(device_class) 

422 else: 

423 device_class = 'Devices/{0}'.format(device_class) 

424 

425 template_uid = '{0}/rrdTemplates/{1}'.format(device_class, template) 

426 return self._router_request( 

427 self._make_request_data( 

428 'deleteTemplate', 

429 dict(uid=template_uid), 

430 ) 

431 ) 

432 

433 def add_local_template(self, zenoss_object, name): 

434 """ 

435 Adds a local template to an object. 

436 

437 Arguments: 

438 zenoss_object (str): Uid of the object to add the local template to 

439 name (str): Unique name for the new local template 

440 

441 Returns: 

442 ZenossTemplate: 

443 """ 

444 if not zenoss_object.startswith('Devices'): 

445 if zenoss_object.startswith('/'): 

446 zenoss_object = 'Devices{0}'.format(zenoss_object) 

447 else: 

448 zenoss_object = 'Devices/{0}'.format(zenoss_object) 

449 

450 template_data = self._router_request( 

451 self._make_request_data( 

452 'addLocalRRDTemplate', 

453 dict( 

454 uid=zenoss_object, 

455 templateName=name, 

456 ) 

457 ) 

458 ) 

459 

460 return self._get_template_by_uid(template_data['nodeConfig']['uid'].replace('/zport/dmd/', '', 1)) 

461 

462 def delete_local_template(self, zenoss_object, name): 

463 """ 

464 Builds the request data for deleting a local template to an object. 

465 

466 Arguments: 

467 object (str): Uid of the object to remove the local template from 

468 name (str): Unique name of the new local template 

469 

470 Returns: 

471 dict: 

472 """ 

473 if not zenoss_object.startswith('Devices'): 

474 if zenoss_object.startswith('/'): 

475 zenoss_object = 'Devices{0}'.format(zenoss_object) 

476 else: 

477 zenoss_object = 'Devices/{0}'.format(zenoss_object) 

478 

479 return self._router_request( 

480 self._make_request_data( 

481 'removeLocalRRDTemplate', 

482 dict( 

483 uid=zenoss_object, 

484 templateName=name, 

485 ) 

486 ) 

487 ) 

488 

489 def get_data_source_types(self): 

490 """ 

491 Gets the list of available data source types. 

492 

493 Returns: 

494 list: 

495 """ 

496 ds_types_data = self._router_request( 

497 self._make_request_data( 

498 'getDataSourceTypes', 

499 dict(query=''), 

500 ) 

501 ) 

502 data_source_types = [] 

503 for ds_type in ds_types_data['data']: 

504 data_source_types.append(ds_type['type']) 

505 

506 return data_source_types 

507 

508 def get_threshold_types(self): 

509 """ 

510 Gets the list of available threshold types. 

511 

512 Returns: 

513 list: 

514 """ 

515 threshold_types_data = self._router_request( 

516 self._make_request_data( 

517 'getThresholdTypes', 

518 dict() 

519 ) 

520 ) 

521 threshold_types = [] 

522 for threshold_type in threshold_types_data['data']: 

523 threshold_types.append(threshold_type['type']) 

524 

525 return threshold_types 

526 

527 def add_data_point_to_graph(self, datapoint, graph, include_thresholds=False): 

528 """ 

529 Adds a data point to a graph. 

530 

531 Arguments: 

532 datapoint (str): Uid of the data point to add 

533 graph (str): Uid of the graph to add the data point to 

534 include_thresholds (bool): Set to True to include the related 

535 thresholds for the data point 

536 

537 Returns: 

538 dict: 

539 """ 

540 return self._router_request( 

541 self._make_request_data( 

542 'addDataPointToGraph', 

543 dict( 

544 dataPointUid=datapoint, 

545 graphUid=graph, 

546 includeThresholds=include_thresholds, 

547 ) 

548 ) 

549 ) 

550 

551 

552class ZenossTemplate(TemplateRouter): 

553 """ 

554 Class for Zenoss Template objects 

555 """ 

556 

557 def __init__(self, url, headers, ssl_verify, template_data): 

558 super(ZenossTemplate, self).__init__(url, headers, ssl_verify) 

559 

560 self.definition = template_data.setdefault('definition', None) 

561 self.description = template_data.setdefault('description', None) 

562 self.hidden = template_data.setdefault('hidden', False) 

563 self.iconCls = template_data.setdefault('iconCls', None) 

564 self.id = template_data.setdefault('id', None) 

565 self.inspector_type = template_data.setdefault('inspector_type', None) 

566 self.leaf = template_data.setdefault('leaf', True) 

567 self.meta_type = template_data.setdefault('meta_type', 'RRDTemplate') 

568 self.name = template_data['name'] 

569 self.qtip = template_data.setdefault('qtip', None) 

570 self.targetPythonClass = template_data.setdefault('targetPythonClass', None) 

571 self.text = template_data.setdefault('text', None) 

572 

573 if 'uid' in template_data: 

574 self.uid = template_data['uid'].replace('/zport/dmd/', '', 1) 

575 else: 

576 self.uid = None 

577 

578 def copy(self, target): 

579 """ 

580 Copy a template to another device or device class. 

581 

582 Arguments: 

583 target (str): Uid of the device or device class to copy to 

584 

585 Returns: 

586 ZenossTemplate: 

587 """ 

588 if not target.endswith('rrdTemplates'): 

589 target = target + '/rrdTemplates' 

590 template_data = self._router_request( 

591 self._make_request_data( 

592 'copyTemplate', 

593 dict( 

594 uid=self.uid, 

595 targetUid=target, 

596 ) 

597 ) 

598 ) 

599 return ZenossTemplate( 

600 self.api_url, 

601 self.api_headers, 

602 self.ssl_verify, 

603 template_data['data'] 

604 ) 

605 

606 def delete(self): 

607 """ 

608 Removes a template. 

609 

610 Returns: 

611 dict: 

612 """ 

613 return self._router_request( 

614 self._make_request_data( 

615 'deleteTemplate', 

616 dict(uid=self.uid), 

617 ) 

618 ) 

619 

620 def get_data_sources(self): 

621 """ 

622 Gets data sources configured for a template. 

623 

624 Returns: 

625 list(ZenossDataSource): 

626 """ 

627 ds_data = self._router_request( 

628 self._make_request_data( 

629 'getDataSources', 

630 dict(uid=self.uid), 

631 ) 

632 ) 

633 datasources = [] 

634 for ds in ds_data['data']: 

635 datasources.append( 

636 ZenossDataSource( 

637 self.api_url, 

638 self.api_headers, 

639 self.ssl_verify, 

640 ds 

641 ) 

642 ) 

643 return datasources 

644 

645 def list_data_sources(self): 

646 """ 

647 Rerturns data sources configured for a template as a list. 

648 

649 Returns: 

650 list(str): 

651 """ 

652 ds_data = self._router_request( 

653 self._make_request_data( 

654 'getDataSources', 

655 dict(uid=self.uid), 

656 ) 

657 ) 

658 datasources = [] 

659 for ds in ds_data['data']: 

660 datasources.append(ds['uid'].replace('/zport/dmd/', '', 1)) 

661 

662 return datasources 

663 

664 def get_data_source(self, datasource): 

665 """ 

666 Get a particular data source. 

667 

668 Arguments: 

669 datasource (str): Name of the data source to get 

670 

671 Returns: 

672 ZenossDataSource: 

673 """ 

674 datasource_uid = '{}/datasources/{}'.format(self.uid, datasource) 

675 

676 return self._get_data_source_by_uid(datasource_uid) 

677 

678 def add_data_source(self, datasource, type): 

679 """ 

680 Adds a data source to a template. 

681 

682 Arguments: 

683 datasource (str): Name of the new data source 

684 type (str): Type of the new data source, must be one of the types 

685 returned by get_data_source_types() 

686 

687 Returns: 

688 ZenossDataSource: 

689 """ 

690 response_data = self._router_request( 

691 self._make_request_data( 

692 'addDataSource', 

693 dict( 

694 templateUid=self.uid, 

695 name=datasource, 

696 type=type, 

697 ) 

698 ) 

699 ) 

700 return self._get_data_source_by_uid( 

701 '{}/datasources/{}'.format(self.uid, datasource) 

702 ) 

703 

704 def delete_data_source(self, datasource): 

705 """ 

706 Deletes a data source from a template. 

707 

708 Arguments: 

709 datasource (str): Name the data source to remove 

710 

711 Returns: 

712 dict: 

713 """ 

714 datasource_uid = '{}/datasources/{}'.format(self.uid, datasource) 

715 return self._router_request( 

716 self._make_request_data( 

717 'deleteDataSource', 

718 dict(uid=datasource_uid), 

719 ) 

720 ) 

721 

722 def get_data_points(self): 

723 """ 

724 Get all the data points in a template. 

725 

726 Returns: 

727 list(ZenossDataPoint): 

728 """ 

729 dp_data = self._router_request( 

730 self._make_request_data( 

731 'getDataPoints', 

732 dict(uid=self.uid) 

733 ) 

734 ) 

735 

736 datapoints = [] 

737 for dp in dp_data['data']: 

738 datapoints.append( 

739 ZenossDataPoint( 

740 self.api_url, 

741 self.api_headers, 

742 self.ssl_verify, 

743 dp 

744 ) 

745 ) 

746 return datapoints 

747 

748 def list_data_points(self): 

749 """ 

750 Returns all the data points in a template as a list. 

751 

752 Returns: 

753 list(str): 

754 """ 

755 dp_data = self._router_request( 

756 self._make_request_data( 

757 'getDataPoints', 

758 dict(uid=self.uid) 

759 ) 

760 ) 

761 

762 datapoints = [] 

763 for dp in dp_data['data']: 

764 datapoints.append(dp['uid'].replace('/zport/dmd/', '', 1)) 

765 

766 return datapoints 

767 

768 def get_thresholds(self): 

769 """ 

770 Gets the thresholds of a template. 

771 

772 Returns: 

773 list(ZenossThresholds): 

774 """ 

775 threshold_data = self._router_request( 

776 self._make_request_data( 

777 'getThresholds', 

778 dict(uid=self.uid), 

779 ) 

780 ) 

781 

782 thresholds = [] 

783 for t in threshold_data['data']: 

784 thresholds.append( 

785 ZenossThreshold( 

786 self.api_url, 

787 self.api_headers, 

788 self.ssl_verify, 

789 t 

790 ) 

791 ) 

792 

793 return thresholds 

794 

795 def list_thresholds(self): 

796 """ 

797 Returns the thresholds of a template as a list. 

798 

799 Returns: 

800 list(str): 

801 """ 

802 threshold_data = self._router_request( 

803 self._make_request_data( 

804 'getThresholds', 

805 dict(uid=self.uid), 

806 ) 

807 ) 

808 

809 thresholds = [] 

810 for t in threshold_data['data']: 

811 thresholds.append(t['uid'].replace('/zport/dmd/', '', 1)) 

812 

813 return thresholds 

814 

815 def get_threshold(self, threshold): 

816 """ 

817 Get a particular threshold. 

818 

819 Arguments: 

820 threshold (str): Name of the threshold to get details on 

821 

822 Returns: 

823 ZenossThreshold: 

824 """ 

825 threshold_uid = '{}/thresholds/{}'.format(self.uid, threshold) 

826 return self._get_threshold_by_uid(threshold_uid) 

827 

828 def add_threshold(self, threshold, threshold_type, datapoints): 

829 """ 

830 Adds a threshold to a template. 

831 

832 Arguments: 

833 threshold (str): Name of the new threshold 

834 threshold_type (str): Type of the new threshold, must be one of the types 

835 returned by get_threshold_types() 

836 datapoints (list): List of datapoints to select for the threshold 

837 

838 Returns: 

839 ZenossThreshold: 

840 """ 

841 if not isinstance(datapoints, list): 

842 raise ZenossAPIClientError('Type error: datapoints to add to a threshold must be a list') 

843 

844 response_data = self._router_request( 

845 self._make_request_data( 

846 'addThreshold', 

847 dict( 

848 uid=self.uid, 

849 thresholdId=threshold, 

850 thresholdType=threshold_type, 

851 dataPoints=datapoints, 

852 ) 

853 ) 

854 ) 

855 

856 return self._get_threshold_by_uid( 

857 '{}/thresholds/{}'.format(self.uid, threshold) 

858 ) 

859 

860 def delete_threshold(self, threshold): 

861 """ 

862 Deletes a threshold. 

863 

864 Arguments: 

865 threshold (str): Name of the threshold to remove 

866 

867 Returns: 

868 dict: 

869 """ 

870 threshold_uid = '{}/thresholds/{}'.format(self.uid, threshold) 

871 return self._router_request( 

872 self._make_request_data( 

873 'removeThreshold', 

874 dict(uid=threshold_uid), 

875 ) 

876 ) 

877 

878 def get_graphs(self): 

879 """ 

880 Get the graphs defined for a template. 

881 

882 Returns: 

883 list(ZenossGraph): 

884 """ 

885 graphs_data = self._router_request( 

886 self._make_request_data( 

887 'getGraphs', 

888 dict(uid=self.uid), 

889 ) 

890 ) 

891 

892 graphs = [] 

893 for g in graphs_data: 

894 graphs.append( 

895 ZenossGraph( 

896 self.api_url, 

897 self.api_headers, 

898 self.ssl_verify, 

899 g 

900 ) 

901 ) 

902 return graphs 

903 

904 def list_graphs(self): 

905 """ 

906 Returns the graphs defined for a template as a list. 

907 

908 Returns: 

909 list(str): 

910 """ 

911 graphs_data = self._router_request( 

912 self._make_request_data( 

913 'getGraphs', 

914 dict(uid=self.uid), 

915 ) 

916 ) 

917 

918 graphs = [] 

919 for g in graphs_data: 

920 graphs.append(g['uid'].replace('/zport/dmd/', '', 1)) 

921 

922 return graphs 

923 

924 def get_graph(self, graph): 

925 """ 

926 Get a particular graph. 

927 

928 Arguments: 

929 graph (str): Name of the graph to get the definition of 

930 

931 Returns: 

932 ZenossGraph: 

933 """ 

934 graph_uid = '{}/graphDefs/{}'.format(self.uid, graph) 

935 

936 return self._get_graph_by_uid(graph_uid) 

937 

938 def add_graph(self, graph): 

939 """ 

940 Add a new graph to a template. 

941 

942 Arguments: 

943 graph (str): Name for the new graph 

944 

945 Returns: 

946 ZenossGraph: 

947 """ 

948 response_data = self._router_request( 

949 self._make_request_data( 

950 'addGraphDefinition', 

951 dict( 

952 templateUid=self.uid, 

953 graphDefinitionId=graph, 

954 ) 

955 ) 

956 ) 

957 

958 return self._get_graph_by_uid( 

959 '{}/graphDefs/{}'.format(self.uid, graph) 

960 ) 

961 

962 def delete_graph(self, graph): 

963 """ 

964 Delete a particular graph. 

965 

966 Arguments: 

967 graph (str): The name of the graph to delete. 

968 

969 Returns: 

970 dict: 

971 """ 

972 graph_uid = '{0}/graphDefs/{1}'.format(self.uid, graph) 

973 

974 return self._router_request( 

975 self._make_request_data( 

976 'deleteGraphDefinition', 

977 data=dict( 

978 uid=graph_uid, 

979 ) 

980 ) 

981 ) 

982 

983 

984class ZenossDataSource(TemplateRouter): 

985 """ 

986 Class for Zenoss template data sources 

987 """ 

988 

989 def __init__(self, url, headers, ssl_verify, ds_data): 

990 super(ZenossDataSource, self).__init__(url, headers, ssl_verify) 

991 

992 uid = ds_data.pop('uid') 

993 self.uid = uid.replace('/zport/dmd/', '', 1) 

994 self.name = ds_data.pop('name') 

995 self.id = ds_data.pop('id') 

996 self.eventClass = ds_data.pop('eventClass') 

997 self.eventKey = ds_data.pop('eventKey') 

998 self.severity = ds_data.pop('severity') 

999 self.type = ds_data.pop('type') 

1000 self.component = ds_data.pop('component') 

1001 self.properties = ds_data 

1002 self.parent = self.uid.split('/datasources/')[0] 

1003 

1004 def delete(self): 

1005 """ 

1006 Deletes a data source from a template. 

1007 

1008 Returns: 

1009 dict: 

1010 """ 

1011 return self._router_request( 

1012 self._make_request_data( 

1013 'deleteDataSource', 

1014 dict(uid=self.uid), 

1015 ) 

1016 ) 

1017 

1018 def get_data_points(self): 

1019 """ 

1020 Get all the data points for a datasource. 

1021 

1022 Returns: 

1023 list(ZenossDataPoint): 

1024 """ 

1025 dp_data = self._router_request( 

1026 self._make_request_data( 

1027 'getDataPoints', 

1028 dict(uid=self.parent) 

1029 ) 

1030 ) 

1031 

1032 datapoints = [] 

1033 for dp in dp_data['data']: 

1034 if dp['name'].startswith(self.name): 

1035 datapoints.append( 

1036 ZenossDataPoint( 

1037 self.api_url, 

1038 self.api_headers, 

1039 self.ssl_verify, 

1040 dp 

1041 ) 

1042 ) 

1043 return datapoints 

1044 

1045 def list_data_points(self): 

1046 """ 

1047 Returns all the data points for a datasource as a list. 

1048 

1049 Returns: 

1050 list(str): 

1051 """ 

1052 dp_data = self._router_request( 

1053 self._make_request_data( 

1054 'getDataPoints', 

1055 dict(uid=self.parent) 

1056 ) 

1057 ) 

1058 

1059 datapoints = [] 

1060 for dp in dp_data['data']: 

1061 if dp['name'].startswith(self.name): 

1062 datapoints.append(dp['uid'].replace('/zport/dmd/', '', 1)) 

1063 

1064 return datapoints 

1065 

1066 def get_data_point(self, datapoint): 

1067 """ 

1068 Get a particular data point. 

1069 

1070 Arguments: 

1071 datapoint (str): Name of the data point to get details for 

1072 

1073 Returns: 

1074 ZenossDataPoint: 

1075 """ 

1076 datapoint_uid = '{}/datapoints/{}'.format(self.uid, datapoint) 

1077 

1078 return self._get_data_point_by_uid(datapoint_uid) 

1079 

1080 def add_data_point(self, datapoint): 

1081 """ 

1082 Adds a data point to a data source. 

1083 

1084 Arguments: 

1085 datapoint (str): Name of the new data point 

1086 

1087 Returns: 

1088 ZenossDataPoint: 

1089 """ 

1090 response_data = self._router_request( 

1091 self._make_request_data( 

1092 'addDataPoint', 

1093 dict( 

1094 dataSourceUid=self.uid, 

1095 name=datapoint, 

1096 ) 

1097 ) 

1098 ) 

1099 

1100 return self._get_data_point_by_uid( 

1101 '{}/datapoints/{}'.format(self.uid, datapoint) 

1102 ) 

1103 

1104 def delete_data_point(self, datapoint): 

1105 """ 

1106 Deletes a data point from a template. 

1107 

1108 Arguments: 

1109 datapoint (str): Name of the data point to remove 

1110 

1111 Returns: 

1112 dict: 

1113 """ 

1114 datapoint_uid = '{}/datapoints/{}'.format(self.uid, datapoint) 

1115 return self._router_request( 

1116 self._make_request_data( 

1117 'deleteDataPoint', 

1118 dict(uid=datapoint_uid), 

1119 ) 

1120 ) 

1121 

1122 

1123class ZenossDataPoint(TemplateRouter): 

1124 """ 

1125 Class for Zenoss data points 

1126 """ 

1127 

1128 def __init__(self, url, headers, ssl_verify, dp_data): 

1129 super(ZenossDataPoint, self).__init__(url, headers, ssl_verify) 

1130 

1131 self.isrow = dp_data['isrow'] 

1132 self.leaf = dp_data['leaf'] 

1133 self.description = dp_data['description'] 

1134 self.rrdmin = dp_data['rrdmin'] 

1135 self.name = dp_data['name'] 

1136 self.rate = dp_data['rate'] 

1137 self.newId = dp_data['newId'] 

1138 self.createCmd = dp_data['createCmd'] 

1139 self.rrdtype = dp_data['rrdtype'] 

1140 self.rrdmax = dp_data['rrdmax'] 

1141 self.aliases = dp_data['aliases'] 

1142 self.type = dp_data['type'] 

1143 self.id = dp_data['id'] 

1144 self.uid = dp_data['uid'].replace('/zport/dmd/', '', 1) 

1145 self.parent = self.uid.split('/datapoints/')[0] 

1146 

1147 def set_threshold(self, threshold, threshold_type): 

1148 """ 

1149 Adds a threshold for the data point 

1150 

1151 Arguments: 

1152 threshold (str): Name of the threshold to add 

1153 threshold_type (str): Type of the new threshold, must be one of the 

1154 types returned by get_threshold_types() 

1155 

1156 Returns: 

1157 ZenossThreshold: 

1158 """ 

1159 template = self.uid.split('/datasources/')[0] 

1160 

1161 response_data = self._router_request( 

1162 self._make_request_data( 

1163 'addThreshold', 

1164 dict( 

1165 uid=template, 

1166 thresholdId=threshold, 

1167 thresholdType=threshold_type, 

1168 dataPoints=[self.uid], 

1169 ) 

1170 ) 

1171 ) 

1172 

1173 return self._get_threshold_by_uid( 

1174 '{}/thresholds/{}'.format(self.uid, threshold) 

1175 ) 

1176 

1177 def make_counter(self): 

1178 """ 

1179 Sets the RRD Type of the data point to COUNTER 

1180 

1181 Returns: 

1182 bool: 

1183 """ 

1184 self.set_properties(dict(rrdtype='COUNTER')) 

1185 self.rrdtype = 'COUNTER' 

1186 

1187 return True 

1188 

1189 def make_gauge(self): 

1190 """ 

1191 Sets the RRD Type of the data point to GAUGE 

1192 

1193 Returns: 

1194 bool: 

1195 """ 

1196 self.set_properties(dict(rrdtype='GAUGE')) 

1197 self.rrdtype = 'GAUGE' 

1198 

1199 return True 

1200 

1201 def add_to_graph(self, graph, include_thresholds=False): 

1202 """ 

1203 Adds a data point to a graph. 

1204 

1205 Arguments: 

1206 graph (str): Name of the graph to add the data point to 

1207 include_thresholds (bool): Set to True to include the related 

1208 thresholds for the data point 

1209 

1210 Returns: 

1211 dict: 

1212 """ 

1213 graph_uid = '{0}/graphDefs/{1}'.format(self.uid.split('/datasources/')[0], graph) 

1214 return self.add_data_point_to_graph(self.uid, graph_uid, include_thresholds) 

1215 

1216 def delete(self): 

1217 """ 

1218 Deletes a data point from a template. 

1219 

1220 Returns: 

1221 dict: 

1222 """ 

1223 return self._router_request( 

1224 self._make_request_data( 

1225 'deleteDataPoint', 

1226 dict(uid=self.uid), 

1227 ) 

1228 ) 

1229 

1230 

1231class ZenossThreshold(TemplateRouter): 

1232 """ 

1233 Class for Zenoss thresholds 

1234 """ 

1235 

1236 def __init__(self, url, headers, ssl_verify, threshold_data): 

1237 super(ZenossThreshold, self).__init__(url, headers, ssl_verify) 

1238 

1239 self.description = threshold_data.pop('description') 

1240 self.enabled = threshold_data.pop('enabled') 

1241 self.name = threshold_data.pop('name') 

1242 self.dataPoints = threshold_data.pop('dataPoints') 

1243 self.eventClass = threshold_data.pop('eventClass') 

1244 self.type = threshold_data.pop('type') 

1245 self.id = threshold_data.pop('id') 

1246 self.severity = threshold_data.pop('severity') 

1247 uid = threshold_data.pop('uid') 

1248 self.uid = uid.replace('/zport/dmd/', '', 1) 

1249 self.parent = self.uid.split('/thresholds/')[0] 

1250 self.properties = threshold_data 

1251 

1252 def set_max(self, maxval): 

1253 """ 

1254 Sets the threshold value for a MinMaxThreshold checking the max 

1255 value of a data point. 

1256 

1257 Arguments: 

1258 maxval (str): Maximum value for the data point before alerting 

1259 

1260 Returns: 

1261 bool: 

1262 """ 

1263 self.set_properties(dict(maxval=maxval)) 

1264 self.properties['maxval'] = maxval 

1265 

1266 return True 

1267 

1268 def set_min(self, minval): 

1269 """ 

1270 Sets the threshold value for a MinMaxThreshold checking the minimum 

1271 value of a data point. 

1272 

1273 Arguments: 

1274 minval (str): Minimum value for the data point before alerting 

1275 

1276 Returns: 

1277 bool: 

1278 """ 

1279 self.set_properties(dict(minval=minval)) 

1280 self.properties['minval'] = minval 

1281 

1282 return True 

1283 

1284 def delete(self): 

1285 """ 

1286 Deletes a threshold. 

1287 

1288 Returns: 

1289 dict: 

1290 """ 

1291 return self._router_request( 

1292 self._make_request_data( 

1293 'removeThreshold', 

1294 dict(uid=self.uid), 

1295 ) 

1296 ) 

1297 

1298 

1299class ZenossGraph(TemplateRouter): 

1300 """ 

1301 Class for Zenoss graphs 

1302 """ 

1303 

1304 def __init__(self, url, headers, ssl_verify, graph_data): 

1305 super(ZenossGraph, self).__init__(url, headers, ssl_verify) 

1306 

1307 self.sequence = graph_data['sequence'] 

1308 self.height = graph_data['height'] 

1309 self.miny = graph_data['miny'] 

1310 self.id = graph_data['id'] 

1311 self.maxy = graph_data['maxy'] 

1312 self.autoscale = graph_data['autoscale'] 

1313 self.log = graph_data['log'] 

1314 self.custom = graph_data['custom'] 

1315 self.width = graph_data['width'] 

1316 self.graphPoints = graph_data['graphPoints'] 

1317 self.rrdVariables = graph_data['rrdVariables'] 

1318 self.units = graph_data['units'] 

1319 self.hasSummary = graph_data['hasSummary'] 

1320 self.ceiling = graph_data['ceiling'] 

1321 self.description = graph_data['description'] 

1322 self.base = graph_data['base'] 

1323 self.name = graph_data['name'] 

1324 self.uid = graph_data['uid'].replace('/zport/dmd/', '', 1) 

1325 self.parent = self.uid.split('/graphDefs/')[0] 

1326 

1327 def delete(self): 

1328 """ 

1329 Delete the graph. 

1330 

1331 Returns: 

1332 dict: 

1333 """ 

1334 return self._router_request( 

1335 self._make_request_data( 

1336 'deleteGraphDefinition', 

1337 data=dict( 

1338 uid=self.uid, 

1339 ) 

1340 ) 

1341 ) 

1342 

1343 def get_points(self): 

1344 """ 

1345 Gets the data points of a graph. 

1346 

1347 Returns: 

1348 list(ZenossDataPoint): 

1349 """ 

1350 points_data = self._router_request( 

1351 self._make_request_data( 

1352 'getGraphPoints', 

1353 dict(uid=self.uid,) 

1354 ) 

1355 ) 

1356 

1357 points = [] 

1358 for p in points_data['data']: 

1359 point_datasource = p['dpName'].split('_')[0] 

1360 points.append( 

1361 self._get_data_point_by_uid('{0}/datasources/{1}/datapoints/{2}'.format(self.parent, point_datasource, p['dpName'])) 

1362 ) 

1363 return points 

1364 

1365 def list_points(self): 

1366 """ 

1367 Returns the data points of a graph as a list. 

1368 

1369 Returns: 

1370 list(str): 

1371 """ 

1372 points_data = self._router_request( 

1373 self._make_request_data( 

1374 'getGraphPoints', 

1375 dict(uid=self.uid,) 

1376 ) 

1377 ) 

1378 

1379 points = [] 

1380 for p in points_data['data']: 

1381 points.append(p['uid'].replace('/zport/dmd/', '', 1)) 

1382 

1383 return points 

1384 

1385 def add_point(self, datasource, datapoint, include_thresholds=False): 

1386 """ 

1387 Adds a data point to a graph. 

1388 

1389 Arguments: 

1390 datasource (str): Name of the data source holding the data point 

1391 datapoint (str): Name of the data point to add 

1392 include_thresholds (bool): Set to True to include the related 

1393 thresholds for the data point 

1394 

1395 Returns: 

1396 dict: 

1397 """ 

1398 datapoint_uid = '{0}/datasources/{1}/datapoints/{2}'.format(self.parent, datasource, datapoint) 

1399 return self.add_data_point_to_graph(datapoint_uid, self.uid, include_thresholds) 

1400 

1401 def set_graph_properties(self, properties): 

1402 """ 

1403 Set the properties for a graph. 

1404 

1405 Arguments: 

1406 properties (dict): Properties and values to set 

1407 

1408 Returns: 

1409 dict: 

1410 """ 

1411 data = dict(uid=self.uid) 

1412 data.update(properties) 

1413 response_data = self._router_request( 

1414 self._make_request_data( 

1415 'setGraphDefinition', 

1416 data, 

1417 ) 

1418 ) 

1419 for prop in properties: 

1420 if getattr(self, prop, False): 

1421 setattr(self, prop, properties[prop]) 

1422 

1423 return response_data 

1424 

1425 def set_zero_baseline(self): 

1426 """ 

1427 Set the minimum value of a graph display to zero. By default 

1428 Zenoss graph scale is dynamic, meaning the display 

1429 can be skewed because the minimum value isn't fixed. 

1430 """ 

1431 return self.set_graph_properties( 

1432 properties=dict(miny=0), 

1433 ) 

1434 

1435 def set_point_sequence(self, datapoints): 

1436 """ 

1437 Sets the order of data points in a graph. 

1438 

1439 Arguments: 

1440 datapoints (list): List of data point names in the desired order 

1441 

1442 Returns: 

1443 dict: 

1444 """ 

1445 if not isinstance(datapoints, list): 

1446 raise ZenossAPIClientError('Type error: Sequence of datapoints must be a list') 

1447 

1448 graph_points = [] 

1449 for p in datapoints: 

1450 graph_points.append('{}/graphPoints/{}'.format(self.uid, p)) 

1451 

1452 return self._router_request( 

1453 self._make_request_data( 

1454 'setGraphPointSequence', 

1455 dict(uids=graph_points) 

1456 ) 

1457 ) 

1458 

1459 def delete_point(self, datapoint): 

1460 """ 

1461 Deletes a data point from a graph. 

1462 

1463 Arguments: 

1464 datapoint (str): Name of the data point to remove 

1465 

1466 Returns: 

1467 dict: 

1468 """ 

1469 response_data = self._router_request( 

1470 self._make_request_data( 

1471 'deleteGraphPoint', 

1472 dict(uid='{}/graphPoints/{}'.format(self.uid, datapoint)) 

1473 ) 

1474 ) 

1475 

1476 graph_data = self._router_request( 

1477 self._make_request_data( 

1478 'getGraphDefinition', 

1479 dict(uid=self.uid) 

1480 ) 

1481 ) 

1482 

1483 self.graphPoints = graph_data['data']['graphPoints'] 

1484 self.rrdVariables = graph_data['data']['rrdVariables'] 

1485 

1486 return response_data 

1487 

1488 def add_graph_threshold(self, threshold): 

1489 """ 

1490 Adds a threshold to a graph. 

1491 

1492 Arguments: 

1493 threshold (str): Uid of the threshold to add 

1494 

1495 Returns: 

1496 dict: 

1497 """ 

1498 response_data = self._router_request( 

1499 self._make_request_data( 

1500 'addThresholdToGraph', 

1501 dict( 

1502 graphUid=self.uid, 

1503 thresholdUid=threshold, 

1504 ) 

1505 ) 

1506 ) 

1507 

1508 graph_data = self._router_request( 

1509 self._make_request_data( 

1510 'getGraphDefinition', 

1511 dict(uid=self.uid) 

1512 ) 

1513 ) 

1514 

1515 self.graphPoints = graph_data['data']['graphPoints'] 

1516 

1517 return response_data