1<?php
2/*
3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004-2021 The Cacti Group                                 |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+
16 | Cacti: The Complete RRDTool-based Graphing Solution                     |
17 +-------------------------------------------------------------------------+
18 | This code is designed, written, and maintained by the Cacti Group. See  |
19 | about.php and/or the AUTHORS file for specific developer information.   |
20 +-------------------------------------------------------------------------+
21 | http://www.cacti.net/                                                   |
22 +-------------------------------------------------------------------------+
23*/
24
25function upgrade_to_1_2_0() {
26	db_install_add_column('user_domains_ldap', array('name' => 'cn_full_name', 'type' => 'varchar(50)', 'NULL' => true, 'default' => ''));
27	db_install_add_column('user_domains_ldap', array('name' => 'cn_email', 'type' => 'varchar(50)', 'NULL' => true, 'default' => ''));
28
29	$poller_exists = db_column_exists('poller', 'processes');
30
31	db_install_add_column('poller', array('name' => 'max_time', 'type' => 'double', 'after' => 'total_time'));
32	db_install_add_column('poller', array('name' => 'min_time', 'type' => 'double', 'after' => 'max_time'));
33	db_install_add_column('poller', array('name' => 'avg_time', 'type' => 'double', 'after' => 'min_time'));
34	db_install_add_column('poller', array('name' => 'total_polls', 'type' => 'int', 'after' => 'avg_time', 'default' => '0'));
35	db_install_add_column('poller', array('name' => 'processes', 'type' => 'int', 'after' => 'total_polls', 'default' => '1'));
36	db_install_add_column('poller', array('name' => 'threads', 'type' => 'double', 'after' => 'processes', 'default' => '1'));
37	db_install_add_column('poller', array('name' => 'sync_interval', 'type' => 'int', 'after' => 'threads', 'default' => '7200'));
38	db_install_add_column('poller', array('name' => 'timezone', 'type' => 'varchar(40)', 'default' => '', 'after' => 'status'));
39	db_install_add_column('poller', array('name' => 'dbsslkey', 'type' => 'varchar(255)', 'after' => 'dbssl'));
40	db_install_add_column('poller', array('name' => 'dbsslcert', 'type' => 'varchar(255)', 'after' => 'dbssl'));
41	db_install_add_column('poller', array('name' => 'dbsslca', 'type' => 'varchar(255)', 'after' => 'dbssl'));
42
43	if (!$poller_exists) {
44		// Take the value from the settings table and translate to
45		// the new Data Collector table settings
46
47		// Ensure value falls in line with what we expect for processes
48		$max_processes = read_config_option('concurrent_processes');
49		if ($max_processes < 1) $max_processes = 1;
50		if ($max_processes > 10) $max_processes = 10;
51
52		// Ensure value falls in line with what we expect for threads
53		$max_threads = read_config_option('max_threads');
54		if ($max_threads < 1) $max_threads = 1;
55		if ($max_threads > 100) $max_threads = 100;
56
57		db_install_execute("UPDATE poller SET processes = $max_processes, threads = $max_threads");
58	}
59
60	db_install_add_column('host', array('name' => 'location', 'type' => 'varchar(40)', 'after' => 'hostname'));
61	db_install_add_key('host', 'index', 'site_id_location', array('site_id', 'location'));
62
63	db_install_add_column('poller_resource_cache', array('name' => 'attributes', 'type' => 'int unsigned', 'default' => '0'));
64	db_install_add_column('external_links', array('name' => 'refresh', 'type' => 'int unsigned'));
65	db_install_add_column('automation_networks', array('name' => 'same_sysname', 'type' => 'char(2)', 'default' => '', 'after' => 'add_to_cacti'));
66
67	db_install_execute("ALTER TABLE user_auth
68		MODIFY COLUMN password varchar(256) NOT NULL DEFAULT ''");
69
70	db_install_execute("ALTER TABLE graph_tree_items
71		MODIFY COLUMN sort_children_type tinyint(3) unsigned NOT NULL DEFAULT '0'");
72
73	db_install_execute('UPDATE graph_templates_graph
74		SET t_title="" WHERE t_title IS NULL or t_title="0"');
75
76	$log_validation_results = db_install_fetch_cell('SELECT value FROM settings WHERE name=\'log_validation\'');
77	$log_validation         = $log_validation_results['data'];
78
79	$log_developer_results  = db_install_fetch_cell('SELECT value FROM settings WHERE name=\'developer_mode\'');
80	$log_developer          = $log_developer_results['data'];
81
82	if ($log_developer !== false && $log_validation === false) {
83		db_install_execute('UPDATE settings
84			SET name="log_validation" WHERE name="developer_mode"');
85	}
86
87	db_install_add_column('automation_networks', array('name' => 'notification_enabled', 'type' => 'char(2)', 'default' => '', 'after' => 'enabled'));
88	db_install_add_column('automation_networks', array('name' => 'notification_email', 'type' => 'varchar(255)', 'default' => "", 'after' => 'notification_enabled'));
89	db_install_add_column('automation_networks', array('name' => 'notification_fromname', 'type' => 'varchar(32)', 'default' => "", 'after' => 'notification_email'));
90	db_install_add_column('automation_networks', array('name' => 'notification_fromemail', 'type' => 'varchar(128)', 'default' => "", 'after' => 'notification_fromname'));
91
92	if (db_table_exists('dsdebug')) {
93		db_install_rename_table('dsdebug','data_debug');
94	}
95
96	if (!db_table_exists('data_debug')) {
97		db_install_execute("CREATE TABLE `data_debug` (
98			`id` int(11) unsigned NOT NULL auto_increment,
99			`started` int(11) NOT NULL DEFAULT '0',
100			`done` int(11) NOT NULL DEFAULT '0',
101			`user` int(11) NOT NULL DEFAULT '0',
102			`datasource` int(11) NOT NULL DEFAULT '0',
103			`info` text NOT NULL DEFAULT '',
104			`issue` text NOT NULL NULL DEFAULT '',
105			PRIMARY KEY (`id`),
106			KEY `user` (`user`),
107			KEY `done` (`done`),
108			KEY `datasource` (`datasource`),
109			KEY `started` (`started`))
110			ROW_FORMAT=Dynamic
111			ENGINE=InnoDB
112			COMMENT = 'Datasource Debugger Information';");
113	}
114
115	// Upgrade debug plugin to core access by removing custom realm
116	$debug_id_reports = db_install_fetch_cell('SELECT id FROM plugin_config WHERE name = \'Debug\'');
117	$debug_id         = $debug_id_reports['data'];
118
119	if ($debug_id !== false && $debug_id > 0) {
120		// Plugin realms are plugin_id + 100
121		$debug_id += 100;
122		db_execute_prepared('DELETE FROM user_auth_realm WHERE realm_id = ?', array($debug_id));
123		db_execute_prepared('DELETE FROM user_auth_group_realm WHERE realm_id = ?', array($debug_id));
124	}
125
126	// Fix data source stats column type
127	$value_parms = db_get_column_attributes('data_source_stats_hourly_last', 'value');
128
129	if (cacti_sizeof($value_parms)) {
130		if ($value_parms[0]['COLUMN_TYPE'] != 'double') {
131			db_install_execute('ALTER TABLE data_source_stats_hourly_last MODIFY COLUMN `value` DOUBLE DEFAULT NULL');
132		}
133	}
134
135	// Resolve issues with bogus templates issue #1761
136	$snmp_queries_results = db_install_fetch_assoc('SELECT id, name
137		FROM snmp_query
138		ORDER BY id');
139	$snmp_queries = $snmp_queries_results['data'];
140
141	if (cacti_sizeof($snmp_queries)) {
142		foreach($snmp_queries as $query) {
143			db_execute_prepared("UPDATE graph_local AS gl
144				INNER JOIN (
145					SELECT graph_template_id
146					FROM graph_local AS gl
147					WHERE snmp_query_id = ?
148					HAVING graph_template_id NOT IN (
149						SELECT graph_template_id
150						FROM snmp_query_graph
151						WHERE snmp_query_id = ?)
152				) AS rs
153				ON gl.graph_template_id=rs.graph_template_id
154				SET snmp_query_id=0, snmp_query_graph_id=0, snmp_index=''",
155				array($query['id'], $query['id']));
156		}
157	}
158
159	$ids_results = db_install_fetch_assoc('SELECT *
160		FROM graph_local
161		WHERE snmp_query_id > 0
162		AND snmp_query_graph_id = 0');
163	$ids = $ids_results['data'];
164
165	if (cacti_sizeof($ids)) {
166		foreach($ids as $id) {
167			$query_graph_id_results = db_install_fetch_cell('SELECT id
168				FROM snmp_query_graph
169				WHERE snmp_query_id = ?
170				AND graph_template_id = ?',
171				array($id['snmp_query_id'], $id['graph_template_id']));
172			$query_graph_id = $query_graph_id_results['data'];
173
174			if (empty($query_graph_id)) {
175				db_execute_prepared('UPDATE graph_local
176					SET snmp_query_id=0, snmp_query_graph_id=0, snmp_index=""
177					WHERE id = ?',
178					array($id['id']));
179			} else {
180				db_execute_prepared('UPDATE graph_local
181					SET snmp_query_graph_id=?
182					WHERE id = ?',
183					array($query_graph_id, $id['id']));
184			}
185		}
186	}
187
188	db_install_execute('UPDATE graph_tree_items
189		SET host_grouping_type = 1
190		WHERE host_id > 0
191		AND host_grouping_type = 0');
192
193	db_install_execute('UPDATE automation_tree_rules
194		SET host_grouping_type = 1
195		WHERE host_grouping_type = 0');
196
197	db_install_execute("UPDATE settings
198		SET value = IF(value = '1', 'on', '')
199		WHERE name = 'hide_console' and value != 'on'");
200
201	db_install_add_column('sites', array('name' => 'zoom', 'type' => 'tinyint', 'unsigned' => true, 'NULL' => true));
202
203	db_install_drop_key('poller_reindex', 'key', 'PRIMARY');
204
205	db_install_add_key('poller_reindex', 'key', 'PRIMARY', array('host_id', 'data_query_id', 'arg1(187)'));
206
207	db_install_add_column('poller', array('name' => 'last_sync', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00'));
208	db_install_add_column('poller', array('name' => 'requires_sync', 'type' => 'char(3)', 'NULL' => false, 'default' => ''));
209
210	db_install_execute('UPDATE poller SET requires_sync = "on" WHERE id != 1');
211
212	db_install_execute('UPDATE host SET status = 0 WHERE disabled = "on"');
213
214	db_install_add_column('host', array('name' => 'deleted', 'type' => 'char(2)', 'default' => '', 'NULL' => true, 'after' => 'device_threads'));
215}
216