|
11 | 11 | ajaxResponse($data); |
12 | 12 | return; |
13 | 13 | } |
| 14 | + |
| 15 | + if ($task == 'export') { |
| 16 | + exportMonitorsJSON(); |
| 17 | + return; |
| 18 | + } |
14 | 19 | } |
15 | 20 |
|
16 | 21 | // Handle legacy action-based requests |
@@ -589,4 +594,83 @@ function queryRequest() { |
589 | 594 |
|
590 | 595 | return $data; |
591 | 596 | } |
| 597 | + |
| 598 | +function exportMonitorsJSON() { |
| 599 | + require_once('includes/Monitor.php'); |
| 600 | + require_once('includes/Zone.php'); |
| 601 | + require_once('includes/Group.php'); |
| 602 | + require_once('includes/Group_Monitor.php'); |
| 603 | + |
| 604 | + if (!canView('Monitors')) { |
| 605 | + ajaxError('Insufficient permissions'); |
| 606 | + return; |
| 607 | + } |
| 608 | + |
| 609 | + // Fields to exclude from export (runtime/instance-specific, not useful for import) |
| 610 | + $exclude_fields = array('Id', 'Deleted', 'ServerId', 'StorageId', 'Sequence', 'ZoneCount'); |
| 611 | + $zone_exclude_fields = array('Id', 'MonitorId'); |
| 612 | + |
| 613 | + // Get all non-deleted visible monitors |
| 614 | + $sql = 'SELECT M.* FROM Monitors AS M WHERE M.Deleted=false ORDER BY M.Sequence ASC'; |
| 615 | + $db_monitors = dbFetchAll($sql); |
| 616 | + |
| 617 | + // Build group name lookup: MonitorId => array of group names |
| 618 | + $group_names_by_monitor = array(); |
| 619 | + $all_groups = ZM\Group::find(); |
| 620 | + $groups_by_id = array(); |
| 621 | + foreach ($all_groups as $G) { |
| 622 | + $groups_by_id[$G->Id()] = $G; |
| 623 | + } |
| 624 | + foreach (ZM\Group_Monitor::find() as $GM) { |
| 625 | + $mid = $GM->MonitorId(); |
| 626 | + $gid = $GM->GroupId(); |
| 627 | + if (isset($groups_by_id[$gid])) { |
| 628 | + if (!isset($group_names_by_monitor[$mid])) { |
| 629 | + $group_names_by_monitor[$mid] = array(); |
| 630 | + } |
| 631 | + $group_names_by_monitor[$mid][] = $groups_by_id[$gid]->Name(); |
| 632 | + } |
| 633 | + } |
| 634 | + |
| 635 | + $export = array( |
| 636 | + 'version' => ZM_VERSION, |
| 637 | + 'exported' => date('c'), |
| 638 | + 'monitors' => array() |
| 639 | + ); |
| 640 | + |
| 641 | + foreach ($db_monitors as $db_row) { |
| 642 | + if (!visibleMonitor($db_row['Id'])) continue; |
| 643 | + |
| 644 | + $Monitor = new ZM\Monitor($db_row); |
| 645 | + |
| 646 | + // Serialize all monitor fields via to_json, then filter |
| 647 | + $monitor_data = json_decode($Monitor->to_json(), true); |
| 648 | + foreach ($exclude_fields as $field) { |
| 649 | + unset($monitor_data[$field]); |
| 650 | + } |
| 651 | + |
| 652 | + // Add group names |
| 653 | + $monitor_data['Groups'] = isset($group_names_by_monitor[$db_row['Id']]) |
| 654 | + ? $group_names_by_monitor[$db_row['Id']] |
| 655 | + : array(); |
| 656 | + |
| 657 | + // Add zones |
| 658 | + $zones = ZM\Zone::find(array('MonitorId' => $db_row['Id'])); |
| 659 | + $monitor_data['Zones'] = array(); |
| 660 | + foreach ($zones as $zone) { |
| 661 | + $zone_data = json_decode($zone->to_json(), true); |
| 662 | + foreach ($zone_exclude_fields as $field) { |
| 663 | + unset($zone_data[$field]); |
| 664 | + } |
| 665 | + $monitor_data['Zones'][] = $zone_data; |
| 666 | + } |
| 667 | + |
| 668 | + $export['monitors'][] = $monitor_data; |
| 669 | + } |
| 670 | + |
| 671 | + header('Content-Type: application/json; charset=utf-8'); |
| 672 | + header('Content-Disposition: attachment; filename="zm_monitors_export.json"'); |
| 673 | + echo json_encode($export, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 674 | + exit(); |
| 675 | +} |
592 | 676 | ?> |
0 commit comments