Created
May 12, 2026 08:09
-
-
Save NiceRath/77d1caedf039c31a3c1408b4d62d712c to your computer and use it in GitHub Desktop.
Script: Zabbix 7.x - Latest Data to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # USAGE: | |
| # 1. Open Zabbix-Server Latest-Data in Browser & apply filters as needed | |
| # 2. Select the entries you want to export (via Mouse-Click & -Drag) - starting from the first Hostname and ending after the "History" | |
| # 3. Copy the selected entries | |
| # 4. Paste the entries into a text-file (make sure your text-editor DOES NOT replace the TAB's with whitespace - tested with 'nano' and 'vi') | |
| # 5. Run the script targeting the newly created text-file | |
| # 6. Verify the CSV-file contents - if your operating system or browser does handle the copy differently - this script could technically could not work. | |
| from sys import argv | |
| DEBUG = False | |
| def _replace_double_quotes(value: str) -> str: | |
| return value.strip().replace('"', "'") | |
| def _build_latest_data(file_lines: list[str]) -> dict: | |
| latest_data = {} | |
| current_row = {} | |
| current_row_part = 0 | |
| for line in file_lines: | |
| if line.strip() == '': | |
| if len(current_row.values()) == 1: | |
| # maintenance-symbol | |
| continue | |
| if 'value' not in current_row: | |
| current_row['value'] = '-' | |
| if len(current_row.values()) == 5: | |
| latest_data[current_row.pop('name')] = current_row | |
| current_row = {} | |
| current_row_part = 0 | |
| continue | |
| current_row_part += 1 | |
| if current_row_part > 3: | |
| continue | |
| if DEBUG: | |
| print(current_row_part, current_row) | |
| if current_row_part == 1: | |
| current_row['name'] = _replace_double_quotes(line) | |
| continue | |
| if current_row_part == 2: | |
| current_row['item'] = _replace_double_quotes(line) | |
| continue | |
| for i, column in enumerate(line.split(' ')): | |
| if i == 0: | |
| current_row['time'] = _replace_double_quotes(column) | |
| elif i == 1: | |
| current_row['value'] = _replace_double_quotes(column) | |
| elif i == 3: | |
| current_row['tags'] = _replace_double_quotes(column) | |
| return latest_data | |
| def _build_csv(latest_data: dict) -> list[str]: | |
| csv_lines = [] | |
| for zabbix_host, row in latest_data.items(): | |
| values = row.values() | |
| if len(values) != 4: | |
| print(f"BAD DATA: host '{zabbix_host}' => {row}") | |
| continue | |
| csv = '","'.join([zabbix_host] + list(values)) | |
| csv_lines.append(f'"{csv}"\n') | |
| return csv_lines | |
| def main(file_in: str, file_out: str) -> None: | |
| with open(file_in, 'r', encoding='utf-8') as f: | |
| data = f.read() | |
| latest_data = _build_latest_data(data.split('\n')) | |
| csv_lines = _build_csv(latest_data) | |
| with open(file_out, 'w', encoding='utf-8') as f: | |
| for line in csv_lines: | |
| f.write(line) | |
| print('\n' + f"DONE! Written to: {file_out}") | |
| if __name__ == '__main__': | |
| if len(argv) != 2: | |
| raise ValueError(""" | |
| USAGE: | |
| 1 > Source file (selected & copy-pasted table-content from zabbix latest-data view) | |
| 2 > Optional: Output file (default = input with added appendix '.csv') | |
| """) | |
| FILE_IN = argv[1] | |
| try: | |
| FILE_OUT = argv[2] | |
| except IndexError: | |
| FILE_OUT = FILE_IN + '.csv' | |
| main(FILE_IN, FILE_OUT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment