-- --------------------------------------------------------
-- PLEASE NOTE THAT THIS SCHEMA SHOULD NOT BE USED 
-- IF YOU ARE USING BOTH
--  * A GATEWAY OLDER THAN GA3.0.5
--  * AUDITING TO THE DATABASE
-- 
-- IF YOU DO SO THEN THE GATEWAY INFORMATION IN
-- THE AUDIT TABLE WILL NOT BE FILLED IN
-- --------------------------------------------------------
create user insights_user;
create database insights_db;
grant all on insights_db.* to 'insights_user'@'localhost' identified by 'geneos_passwd' with grant option;
grant all on insights_db.* to 'insights_user'@'%' identified by 'geneos_passwd' with grant option;

flush privileges;

-- --------------------------------------------------------
-- insert your database name after the use command below
-- --------------------------------------------------------
use insights_db;
-- --------------------------------------------------------
-- System Tables dont touch
-- --------------------------------------------------------
DROP TABLE IF EXISTS `version_table`;
DROP TABLE IF EXISTS `node_ref_table`;
DROP TABLE IF EXISTS `var_ref_table`;
DROP TABLE IF EXISTS `event_table`;
DROP TABLE IF EXISTS `attribute_table`;
DROP TABLE IF EXISTS `hostname_table`;
DROP TABLE IF EXISTS `audit_table`;
DROP TABLE IF EXISTS `audit_details_table`;
DROP TABLE IF EXISTS `snooze_table`;
DROP TABLE IF EXISTS `user_assignment_table`;
DROP TABLE IF EXISTS `action_table`;
DROP TABLE IF EXISTS `alert_table`;
DROP TABLE IF EXISTS `active_table`;
-- --------------------------------------------------------
-- User tables
-- --------------------------------------------------------
DROP TABLE IF EXISTS `cpu_table`;
DROP TABLE IF EXISTS `disk_table`;
DROP TABLE IF EXISTS `hardware_table`;

-- --------------------------------------------------------
-- Table structure for table `version_table`
-- --------------------------------------------------------
CREATE TABLE `version_table` (
  `major` int(11) default NULL,
  `minor` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO `version_table` (`major`, `minor`) VALUES (1, 8);

-- --------------------------------------------------------
-- Table structure for table `node_ref_table`
-- --------------------------------------------------------
CREATE TABLE `node_ref_table` (
  `ref` int(11) NOT NULL auto_increment,
  `gateway` varchar(50) collate latin1_general_ci default NULL,
  `node` varchar(200) collate latin1_general_ci default NULL,
  PRIMARY KEY  (`ref`),
  UNIQUE KEY `node_ref_index` (`node`,`gateway`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- --------------------------------------------------------
-- Table structure for table `var_ref_table`
-- --------------------------------------------------------
CREATE TABLE `var_ref_table` (
  `ref` int(11) NOT NULL auto_increment,
  `varname` varchar(200) collate latin1_general_ci default NULL,
  `tablename` varchar(50) collate latin1_general_ci default NULL,
  PRIMARY KEY  (`ref`),
  UNIQUE KEY `var_ref_index` (`varname`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- --------------------------------------------------------
-- Table structure for table `event_table`
-- --------------------------------------------------------
CREATE TABLE `event_table` (
  `ref` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `node_ref` int(11) NOT NULL,
  `varname` varchar(200) collate latin1_general_ci NOT NULL,
  `severity` int(11) NOT NULL,
  `description` varchar(250) collate latin1_general_ci default NULL,
  `gateway` varchar(200) default NULL,
  `probe` varchar(200) default NULL,
  `managed_entity` varchar(200) default NULL,
  `sampler` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `dataview` varchar(250) default NULL,
  `headline` varchar(200) default NULL,
  `rowname` varchar(200) default NULL,
  `columnname` varchar(200) default NULL,
  PRIMARY KEY  (`ref`),
  KEY `event_index` (`timestamp`,`node_ref`,`varname`,`severity`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- --------------------------------------------------------
-- Table structure for table `audit_table`
-- --------------------------------------------------------
CREATE TABLE `audit_table` (
  `audit_record_id` varchar(36) default NULL,
  `timestamp` int(11) NOT NULL,
  `username` varchar(50) default NULL,
  `workstation` varchar(50) default NULL,
  `module` varchar(50) default NULL,
  `managedEntity` varchar(200) default NULL,
  `description` varchar(250) default NULL,
  `node_ref` int(11) NOT NULL,
  `gateway` varchar(200) default NULL,
  `probe` varchar(200) default NULL,
  `sampler` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `dataview` varchar(250) default NULL,
  `headline` varchar(200) default NULL,
  `rowname` varchar(200) default NULL,
  `columnname` varchar(200) default NULL,
  `auditevent` varchar(50) default NULL,
  `failreason` varchar(200) default NULL,
  KEY `audit_record_id_index` (`audit_record_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- --------------------------------------------------------
-- Table structure for table `audit_details_table`
-- --------------------------------------------------------
CREATE TABLE `audit_details_table` (
  `audit_record_id` varchar(36) NOT NULL,
  `item` varchar(200) NOT NULL,
  `field` varchar(200) NOT NULL,
  `value` varchar(1000) default NULL,
  KEY `audit_details_index` (`audit_record_id`,`item`,`field`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- --------------------------------------------------------
-- Table structure for table `attribute_table`
-- --------------------------------------------------------
CREATE TABLE `attribute_table` (
  `node_ref` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `value` varchar(255) NOT NULL,
  PRIMARY KEY  (`node_ref`,`name`),
  KEY `attribute_index` (`name`,`value`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- --------------------------------------------------------
-- Table structure for table `hostname_table`
-- --------------------------------------------------------
CREATE TABLE `hostname_table` (
  `node_ref` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY  (`node_ref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- --------------------------------------------------------
-- Table structure for table `snooze_table`
-- --------------------------------------------------------
CREATE TABLE `snooze_table` (
  `ref` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `gateway` varchar(200) default NULL,
  `probe` varchar(200) default NULL,
  `managedEntity` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `sampler` varchar(200) default NULL,
  `dataview` varchar(250) default NULL,
  `headline` varchar(200) default NULL,
  `rowname` varchar(200) default NULL,
  `columnname` varchar(200) default NULL,
  `snoozed`  char(1) NOT NULL CHECK (snoozed IN ('Y', 'N')),
  `snoozedBy` varchar(200) default NULL,
  `description` varchar(250) default NULL,
  `period` varchar(200) default NULL,
  `untilSeverity` int default NULL,
  `untilTime` int(11) default NULL,
  `untilValue` varchar(250) default NULL,
  `unsnoozedBy` varchar(200) default NULL,
  KEY `snooze_ref_index` (`ref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- Necessary because above constraint isn't actually enforced by MySQL at present
DROP TRIGGER IF EXISTS chk_snoozed;

DELIMITER $$
CREATE TRIGGER chk_snoozed BEFORE INSERT ON snooze_table FOR EACH ROW
BEGIN 
  IF NEW.snoozed NOT IN ('Y', 'N') THEN
    call  You_may_not_insert_values_other_than_Y_or_N_err; 
  END IF;
END$$
DELIMITER ;

DROP TRIGGER IF EXISTS chk_snoozed_update;
DELIMITER $$
CREATE TRIGGER chk_snoozed_update BEFORE UPDATE ON snooze_table FOR EACH ROW
BEGIN
  IF (NEW.snoozed NOT IN ('Y', 'N')) THEN
     call  You_may_not_insert_values_other_than_Y_or_N_err; 
  END IF;
END$$
DELIMITER ;

-- --------------------------------------------------------
-- Table structure for table `user_assignment_table`
-- --------------------------------------------------------
CREATE TABLE `user_assignment_table` (
  `ref` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `gateway` varchar(200) default NULL,
  `probe` varchar(200) default NULL,
  `managedEntity` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `sampler` varchar(200) default NULL,
  `dataview` varchar(250) default NULL,
  `headline` varchar(200) default NULL,
  `rowname` varchar(200) default NULL,
  `columnname` varchar(200) default NULL,
  `userAssigned`  char(1) NOT NULL CHECK (userAssigned IN ('Y', 'N')),
  `assignedTo` varchar(200) default NULL,
  `description` varchar(250) default NULL,
  `period` varchar(200) default NULL,
  `untilSeverity` int default NULL,
  `untilTime` int(11) default NULL,
  `untilValue` varchar(250) default NULL,
  `assignedBy` varchar(200) default NULL,
  `unassignedBy` varchar(200) default NULL,
  KEY `snooze_ref_index` (`ref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- Necessary because above constraint isn't actually enforced by MySQL at present
DROP TRIGGER IF EXISTS check_assigned;
DELIMITER $$
CREATE TRIGGER check_assigned BEFORE INSERT ON user_assignment_table FOR EACH ROW
BEGIN 
  IF NEW.userAssigned NOT IN ('Y', 'N') THEN
    call  You_may_not_insert_values_other_than_Y_or_N_err; 
  END IF;
END$$
DELIMITER ;

DROP TRIGGER IF EXISTS check_assigned_update;
DELIMITER $$
CREATE TRIGGER check_assigned_update BEFORE UPDATE ON user_assignment_table FOR EACH ROW
BEGIN
  IF (NEW.userAssigned NOT IN ('Y', 'N')) THEN
     call  You_may_not_insert_values_other_than_Y_or_N_err; 
  END IF;
END$$
DELIMITER ;

-- --------------------------------------------------------
-- Table structure for table `action_table`
-- --------------------------------------------------------
CREATE TABLE `action_table` (
  `ref` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `action` varchar(200) default NULL,
  `actionType` varchar(200) default NULL,
  `source` varchar(250) default NULL,
  `parentAction` varchar(200) default NULL,
  `gateway` varchar(200) default NULL,
  `probe` varchar(200) default NULL,
  `managed_entity` varchar(200) default NULL,
  `sampler` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `dataview` varchar(250) default NULL,
  `headline` varchar(200) default NULL,
  `rowname` varchar(200) default NULL,
  `columnname` varchar(200) default NULL,
  KEY `action_ref_index` (`ref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- --------------------------------------------------------
-- Table structure for table `alert_table`
-- --------------------------------------------------------
CREATE TABLE `alert_table` (
  `ref` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `name` varchar(250) default NULL,
  `hierarchy` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `severity` varchar(200) default NULL,
  `effect` varchar(200) default NULL,
  `repeatCount` int default NULL,
  `escalationLevel` int default NULL,
  KEY `alert_ref_index` (`ref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;


-- --------------------------------------------------------
-- Table structure for table `active_table`
-- --------------------------------------------------------
CREATE TABLE `active_table` (
  `ref` int(11) NOT NULL auto_increment,
  `timestamp` int(11) NOT NULL,
  `gateway` varchar(200) default NULL,
  `probe` varchar(200) default NULL,
  `managedEntity` varchar(200) default NULL,
  `type` varchar(200) default NULL,
  `sampler` varchar(200) default NULL,
  `dataview` varchar(250) default NULL,
  `headline` varchar(200) default NULL,
  `rowname` varchar(200) default NULL,
  `columnname` varchar(200) default NULL,
  `active`  char(1) NOT NULL CHECK (userAssigned IN ('Y', 'N')),
  `source` varchar(250) default NULL,
  KEY `active_ref_index` (`ref`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

-- Necessary because above constraint isn't actually enforced by MySQL at present
DROP TRIGGER IF EXISTS check_active;
DELIMITER $$
CREATE TRIGGER check_active BEFORE INSERT ON active_table FOR EACH ROW
BEGIN 
  IF NEW.active NOT IN ('Y', 'N') THEN
    call  You_may_not_insert_values_other_than_Y_or_N_err; 
  END IF;
END$$
DELIMITER ;

DROP TRIGGER IF EXISTS check_active_update;
DELIMITER $$
CREATE TRIGGER check_active_update BEFORE UPDATE ON active_table FOR EACH ROW
BEGIN
  IF (NEW.active NOT IN ('Y', 'N')) THEN
     call  You_may_not_insert_values_other_than_Y_or_N_err; 
  END IF;
END$$
DELIMITER ;

-- --------------------------------------------------------
-- Table structure for table `cpu_table`
-- --------------------------------------------------------
CREATE TABLE `cpu_table` (
  `timestamp` int(11) NOT NULL,
  `node_ref` int(11) NOT NULL,
  `var_ref` int(11) NOT NULL,
  `value` varchar(250) collate latin1_general_ci default NULL,
  KEY `cpu_index` (`node_ref`,`var_ref`,`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- --------------------------------------------------------
-- Table structure for table `disk_table`
-- --------------------------------------------------------
CREATE TABLE `disk_table` (
  `timestamp` int(11) NOT NULL,
  `node_ref` int(11) NOT NULL,
  `var_ref` int(11) NOT NULL,
  `value` varchar(250) collate latin1_general_ci default NULL,
  KEY `disk_index` (`node_ref`,`var_ref`,`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- --------------------------------------------------------
-- Table structure for table `hardware_table`
-- --------------------------------------------------------
CREATE TABLE `hardware_table` (
  `timestamp` int(11) NOT NULL,
  `node_ref` int(11) NOT NULL,
  `var_ref` int(11) NOT NULL,
  `value` varchar(250) collate latin1_general_ci default NULL,
  KEY `hardware_index` (`node_ref`,`var_ref`,`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;