Repository navigation
Adopt plugin table API and relocate schema to includes/database.php #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3739118
Relocate schema to includes/database.php via plugin table API + db_up…
a29d25d
CI: allowlist unmeasured schema bootstrap and web entry points
e296305
CI: keep include_once for the settings-page-only lib/reports.php load…
2a8d9e3
Add manifest.json + upgrade-time file pruning; consolidate themes/ in…
5ba5c27
database: keep user-owned dashboards table across uninstall
TheWitness 80767e3
Prune phpunit.xml on upgrade and ignore markdown-lint configs
b101c0b
Move libraries into includes/, flatten css, drop sonar config
b4bc6fc
Rename includes/db_functions.php to includes/functions.php
22e3078
Regenerate cacti.pot for the includes/ file relocations
0132b01
Harden prune against tampered-manifest path traversal
3f2e859
docs: update and align the Project Structure block
dda3b2d
docs/tests: standardize test header and correct manifest-contract notes
8d7cf06
refactor: rename prune helpers to the plugin-slug function prefix
ec01c1c
chore: set the Cacti compatibility floor to 1.2.29
916577f
Bump plugin version to 3.0 so the upgrade-time cleanup migration runs
TheWitness File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| <?php | ||
| /* | ||
| +-------------------------------------------------------------------------+ | ||
| | Copyright (C) 2004-2026 The Cacti Group | | ||
| | | | ||
| | This program is free software; you can redistribute it and/or | | ||
| | modify it under the terms of the GNU General Public License | | ||
| | as published by the Free Software Foundation; either version 2 | | ||
| | of the License, or (at your option) any later version. | | ||
| | | | ||
| | This program is distributed in the hope that it will be useful, | | ||
| | but WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | ||
| | GNU General Public License for more details. | | ||
| +-------------------------------------------------------------------------+ | ||
| | Cacti: The Complete RRDtool-based Graphing Solution | | ||
| +-------------------------------------------------------------------------+ | ||
| | This code is designed, written, and maintained by the Cacti Group. See | | ||
| | about.php and/or the AUTHORS file for specific developer information. | | ||
| +-------------------------------------------------------------------------+ | ||
| | http://www.cacti.net/ | | ||
| +-------------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| /** | ||
| * The plugin_monitor_notify_history table definition (one row per | ||
| * notification event). Shared by the create and upgrade paths so both | ||
| * stay in sync from a single source. | ||
| * | ||
| * @return array<string,mixed> Table definition consumed by | ||
| * api_plugin_db_table_create()/db_update_table(). | ||
| */ | ||
| function monitor_notify_history_table_data(): array { | ||
| $data = []; | ||
| $data['columns'][] = ['name' => 'id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => false, 'auto_increment' => true]; | ||
| $data['columns'][] = ['name' => 'host_id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => true, 'default' => null]; | ||
| $data['columns'][] = ['name' => 'notify_type', 'type' => 'tinyint(3)', 'unsigned' => true, 'NULL' => true, 'default' => null]; | ||
| $data['columns'][] = ['name' => 'ping_time', 'type' => 'double', 'NULL' => true, 'default' => null]; | ||
| $data['columns'][] = ['name' => 'ping_threshold', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => true, 'default' => null]; | ||
| $data['columns'][] = ['name' => 'notification_time', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; | ||
| $data['columns'][] = ['name' => 'notes', 'type' => 'varchar(255)', 'NULL' => true, 'default' => null]; | ||
| $data['primary'] = ['id']; | ||
| $data['unique_keys'][] = ['name' => 'unique_key', 'columns' => ['host_id', 'notify_type', 'notification_time']]; | ||
| $data['type'] = 'InnoDB'; | ||
| $data['comment'] = 'Stores Notification Event History'; | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * The plugin_monitor_reboot_history table definition (one row per detected | ||
| * device reboot). | ||
| * | ||
| * @return array<string,mixed> Table definition. | ||
| */ | ||
| function monitor_reboot_history_table_data(): array { | ||
| $data = []; | ||
| $data['columns'][] = ['name' => 'id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => false, 'auto_increment' => true]; | ||
| $data['columns'][] = ['name' => 'host_id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => true, 'default' => null]; | ||
| $data['columns'][] = ['name' => 'reboot_time', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; | ||
| $data['columns'][] = ['name' => 'log_time', 'type' => 'timestamp', 'NULL' => false, 'default' => 'CURRENT_TIMESTAMP']; | ||
| $data['primary'] = ['id']; | ||
| $data['keys'][] = ['name' => 'host_id', 'columns' => ['host_id']]; | ||
| $data['keys'][] = ['name' => 'log_time', 'columns' => ['log_time']]; | ||
| $data['keys'][] = ['name' => 'reboot_time', 'columns' => ['reboot_time']]; | ||
| $data['type'] = 'InnoDB'; | ||
| $data['comment'] = 'Keeps Track of Device Reboot Times'; | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * The plugin_monitor_uptime table definition (last known uptime per | ||
| * device). Uses the current column layout (uptime NOT NULL). | ||
| * | ||
| * @return array<string,mixed> Table definition. | ||
| */ | ||
| function monitor_uptime_table_data(): array { | ||
| $data = []; | ||
| $data['columns'][] = ['name' => 'host_id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => false, 'default' => 0]; | ||
| $data['columns'][] = ['name' => 'uptime', 'type' => 'bigint(20)', 'unsigned' => true, 'NULL' => false, 'default' => 0]; | ||
| $data['primary'] = ['host_id']; | ||
| $data['keys'][] = ['name' => 'uptime', 'columns' => ['uptime']]; | ||
| $data['type'] = 'InnoDB'; | ||
| $data['comment'] = 'Keeps Track of the Devices last uptime to track agent restarts and reboots'; | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * The plugin_monitor_dashboards table definition (per-user predefined | ||
| * dashboards). | ||
| * | ||
| * @return array<string,mixed> Table definition. | ||
| */ | ||
| function monitor_dashboards_table_data(): array { | ||
| $data = []; | ||
| $data['columns'][] = ['name' => 'id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => false, 'auto_increment' => true]; | ||
| $data['columns'][] = ['name' => 'user_id', 'type' => 'int(10)', 'unsigned' => true, 'NULL' => true, 'default' => 0]; | ||
| $data['columns'][] = ['name' => 'name', 'type' => 'varchar(128)', 'NULL' => true, 'default' => '']; | ||
| $data['columns'][] = ['name' => 'url', 'type' => 'varchar(1024)', 'NULL' => true, 'default' => '']; | ||
| $data['primary'] = ['id']; | ||
| $data['keys'][] = ['name' => 'user_id', 'columns' => ['user_id']]; | ||
| $data['type'] = 'InnoDB'; | ||
| $data['comment'] = 'Stores predefined dashboard information for a user or users'; | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * Removes the plugin_db_changes 'create' ownership record for the user-owned | ||
| * dashboards table so api_plugin_db_changes_remove() (run by | ||
| * api_plugin_uninstall()) does not drop it, preserving user dashboards across | ||
| * an uninstall. | ||
| * | ||
| * @return void | ||
| */ | ||
| function monitor_disown_dashboards_table() { | ||
| db_execute("DELETE FROM plugin_db_changes | ||
| WHERE plugin = 'monitor' | ||
| AND `table` = 'plugin_monitor_dashboards' | ||
| AND method = 'create'"); | ||
| } | ||
|
|
||
| /** | ||
| * Creates this plugin's own tables through Cacti's tracked plugin table | ||
| * API and ensures the core host table carries the monitoring columns this | ||
| * plugin relies on. Called from plugin_monitor_install() and (idempotently) | ||
| * from monitor_check_upgrade(). | ||
| * | ||
| * @return void | ||
| */ | ||
| function monitor_setup_table() { | ||
| api_plugin_db_table_create('monitor', 'plugin_monitor_notify_history', monitor_notify_history_table_data()); | ||
| api_plugin_db_table_create('monitor', 'plugin_monitor_reboot_history', monitor_reboot_history_table_data()); | ||
| api_plugin_db_table_create('monitor', 'plugin_monitor_uptime', monitor_uptime_table_data()); | ||
| api_plugin_db_table_create('monitor', 'plugin_monitor_dashboards', monitor_dashboards_table_data()); | ||
|
|
||
| /* dashboards holds user data: create it but do not record plugin ownership, so uninstall preserves it. */ | ||
| monitor_disown_dashboards_table(); | ||
|
|
||
| if (db_table_exists('host')) { | ||
| $row_format = db_fetch_cell("SELECT ROW_FORMAT | ||
| FROM information_schema.tables | ||
| WHERE TABLE_SCHEMA = DATABASE() | ||
| AND TABLE_NAME = 'host'"); | ||
|
|
||
| if (strtoupper((string) $row_format) !== 'DYNAMIC') { | ||
| db_execute('ALTER TABLE host ROW_FORMAT=DYNAMIC'); | ||
| } | ||
| } | ||
|
|
||
| db_execute('SET SESSION innodb_strict_mode=0'); | ||
|
|
||
| api_plugin_db_add_column('monitor', 'host', ['name' => 'monitor', 'type' => 'char(3)', 'NULL' => true, 'default' => 'on']); | ||
| api_plugin_db_add_column('monitor', 'host', ['name' => 'monitor_text', 'type' => 'text', 'NULL' => false]); | ||
| api_plugin_db_add_column('monitor', 'host', ['name' => 'monitor_criticality', 'type' => 'tinyint', 'unsigned' => true, 'NULL' => false, 'default' => '0']); | ||
| api_plugin_db_add_column('monitor', 'host', ['name' => 'monitor_warn', 'type' => 'double', 'NULL' => false, 'default' => '0']); | ||
| api_plugin_db_add_column('monitor', 'host', ['name' => 'monitor_alert', 'type' => 'double', 'NULL' => false, 'default' => '0']); | ||
| api_plugin_db_add_column('monitor', 'host', ['name' => 'monitor_icon', 'type' => 'varchar(30)', 'NULL' => false, 'default' => '']); | ||
|
|
||
| db_execute('SET SESSION innodb_strict_mode=1'); | ||
| } | ||
|
|
||
| /** | ||
| * Refreshes this plugin's own tables to their current definition on | ||
| * upgrade: db_update_table() diffs the live schema against the definition | ||
| * and issues the exact ALTER when the table already exists, otherwise the | ||
| * table is created outright. Called from monitor_check_upgrade(). | ||
| * | ||
| * @return void | ||
| */ | ||
| function monitor_upgrade_tables() { | ||
| $tables = [ | ||
| 'plugin_monitor_notify_history' => monitor_notify_history_table_data(), | ||
| 'plugin_monitor_reboot_history' => monitor_reboot_history_table_data(), | ||
| 'plugin_monitor_uptime' => monitor_uptime_table_data(), | ||
| 'plugin_monitor_dashboards' => monitor_dashboards_table_data(), | ||
| ]; | ||
|
|
||
| foreach ($tables as $table => $table_data) { | ||
| if (db_table_exists($table)) { | ||
| db_update_table($table, $table_data); | ||
| } else { | ||
| api_plugin_db_table_create('monitor', $table, $table_data); | ||
| } | ||
| } | ||
|
|
||
| monitor_disown_dashboards_table(); | ||
| } | ||
|
|
||
| /** | ||
| * Drops the tables this plugin owns that hold transient monitoring state | ||
| * (notification/reboot history and uptime). The user-defined dashboards | ||
| * table is intentionally left in place. Called from | ||
| * plugin_monitor_uninstall(). | ||
| * | ||
| * @return void | ||
| */ | ||
| function monitor_drop_tables() { | ||
| db_execute('DROP TABLE IF EXISTS plugin_monitor_notify_history'); | ||
| db_execute('DROP TABLE IF EXISTS plugin_monitor_reboot_history'); | ||
| db_execute('DROP TABLE IF EXISTS plugin_monitor_uptime'); | ||
| } | ||
|
TheWitness marked this conversation as resolved.
|
||
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.