START TRANSACTION;

SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

SET NAMES utf8mb4;

DROP TABLE IF EXISTS `GROUPS`;
CREATE TABLE `GROUPS` (
  `ID_G` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(40) NOT NULL,
  PRIMARY KEY (`ID_G`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `MAIL`;
CREATE TABLE `MAIL` (
  `ID_M` int(11) NOT NULL AUTO_INCREMENT,
  `ID_SERV` int(11) NOT NULL,
  `Type` varchar(20) NOT NULL,
  `Msg` varchar(255) NOT NULL,
  `Solved` tinyint(4) NOT NULL DEFAULT '0',
  `Sent` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID_M`),
  KEY `ID_SERV` (`ID_SERV`),
  CONSTRAINT `MAIL_ibfk_1` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `P_ALERTS`;
CREATE TABLE `P_ALERTS` (
  `ID_A` int(11) NOT NULL AUTO_INCREMENT,
  `ID_SERV` int(11) NOT NULL,
  `Type` varchar(15) NOT NULL,
  `Param` varchar(20) NOT NULL,
  `Timestamp` varchar(12) NOT NULL,
  `Msg` varchar(255) NOT NULL,
  PRIMARY KEY (`ID_A`),
  UNIQUE KEY `ID_SERV_Type_Param` (`ID_SERV`,`Type`,`Param`),
  CONSTRAINT `P_ALERTS_ibfk_2` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `SERVERS`;
CREATE TABLE `SERVERS` (
  `ID_SERV` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(40) NOT NULL,
  `Description` varchar(255) NOT NULL,
  `IP` varchar(18) NOT NULL,
  `SO` varchar(20) NOT NULL,
  `Version` varchar(70) NULL,
  `Group` int(11) DEFAULT NULL,
  `User` varchar(40) DEFAULT NULL,
  `Password` varchar(64) DEFAULT NULL,
  `IV` varchar(32) DEFAULT NULL,
  `BadCreds` tinyint(4) DEFAULT '0',
  `SSHPort` smallint(6) DEFAULT NULL,
  `Enabled` int(11) DEFAULT '1',
  `Online` int(11) DEFAULT '1',
  `Uptime` int(11) DEFAULT '0',
  `LastCheck` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`ID_SERV`),
  KEY `Group` (`Group`),
  CONSTRAINT `SERVERS_ibfk_2` FOREIGN KEY (`Group`) REFERENCES `GROUPS` (`ID_G`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `SYS`;
CREATE TABLE `SYS` (
  `Option` varchar(20) NOT NULL,
  `Value` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `S_HDDSTAT`;
CREATE TABLE `S_HDDSTAT` (
  `ID_SERV` int(11) NOT NULL,
  `Timestamp` int(11) NOT NULL,
  `HDD` varchar(40) NOT NULL,
  `Space` bigint(20) DEFAULT NULL,
  `Freespace` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`ID_SERV`,`Timestamp`,`HDD`),
  CONSTRAINT `S_HDDSTAT_ibfk_1` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `S_HISTPING`;
CREATE TABLE `S_HISTPING` (
  `ID_SERV` int(11) NOT NULL,
  `Timestamp` int(11) NOT NULL,
  `Value` int(11) NOT NULL DEFAULT '-1',
  PRIMARY KEY (`ID_SERV`,`Timestamp`),
  CONSTRAINT `S_HISTPING_ibfk_1` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `S_HISTRAM`;
CREATE TABLE `S_HISTRAM` (
  `ID_SERV` int(11) NOT NULL,
  `Timestamp` int(11) NOT NULL,
  `Freeram` bigint(20) NOT NULL,
  `Detram` bigint(20) NOT NULL,
  PRIMARY KEY (`ID_SERV`,`Timestamp`,`Freeram`),
  CONSTRAINT `S_HISTRAM_ibfk_1` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `S_HISTSERVICES`;
CREATE TABLE `S_HISTSERVICES` (
  `ID_SERV` int(11) NOT NULL,
  `Name` varchar(40) NOT NULL,
  `Timestamp` int(11) NOT NULL,
  `Status` int(11) NOT NULL,
  PRIMARY KEY (`ID_SERV`,`Name`,`Timestamp`),
  CONSTRAINT `S_HISTSERVICES_ibfk_1` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `S_INGROUP`;
CREATE TABLE `S_INGROUP` (
  `ID_SERV` int(11) NOT NULL,
  `ID_G` int(11) NOT NULL,
  UNIQUE KEY `ID_SERV_ID_G` (`ID_SERV`,`ID_G`),
  KEY `ID_G` (`ID_G`),
  CONSTRAINT `S_INGROUP_ibfk_3` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `S_INGROUP_ibfk_4` FOREIGN KEY (`ID_G`) REFERENCES `GROUPS` (`ID_G`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `S_SERVICES`;
CREATE TABLE `S_SERVICES` (
  `ID_SERV` int(11) NOT NULL,
  `Name` varchar(40) NOT NULL,
  `Type` varchar(15) NOT NULL,
  `Description` varchar(40) DEFAULT NULL,
  `Enabled` int(11) DEFAULT '1',
  `Status` int(11) DEFAULT '1',
  PRIMARY KEY (`ID_SERV`,`Name`),
  CONSTRAINT `S_SERVICES_ibfk_1` FOREIGN KEY (`ID_SERV`) REFERENCES `SERVERS` (`ID_SERV`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


DROP TABLE IF EXISTS `USERS`;
CREATE TABLE `USERS` (
  `ID_U` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(40) NOT NULL,
  `Password` varchar(100) NOT NULL,
  `Email` varchar(40) NOT NULL,
  `SendMail` tinyint(4) NOT NULL,
  `Enabled` int(11) DEFAULT '1',
  `PE_editserv` int(11) DEFAULT '0',
  `PE_admin` int(11) DEFAULT '0',
  PRIMARY KEY (`ID_U`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


INSERT INTO `SYS` (`Option`, `Value`) VALUES
('HDDALERT',	'5'), ('RAMALERT','5'),('AUTODELETE','30');

COMMIT;
