diff --git a/Bash/NucelarMonitor.debian.script.sh b/Bash/NucelarMonitor.debian.script.sh index 802f5e5..136af70 100755 --- a/Bash/NucelarMonitor.debian.script.sh +++ b/Bash/NucelarMonitor.debian.script.sh @@ -25,6 +25,28 @@ function get_net_data(){ }') } +function get_cpu_data(){ + read -r _ u n s id io ir sir st _ < /proc/stat + echo $((u+n+s+id+io+ir+sir+st)) $((id+io)) +} + +function get_cpu_value(){ + + local total_base=$1 + local idle_base=$2 + local total= + local idle= + local cpu= + + read -r total idle <<< $(get_cpu_data) + cpu=$(awk -v total_base="$total_base" -v idle_base="$idle_base" -v total="$total" -v idle="$idle" 'BEGIN{ + total_final = total - total_base; + printf "%.6f", 100 * (total_final - (idle - idle_base)) / total_final; + }') + + echo $cpu $total $idle +} + function execute(){ local domain=$(hostname -d) @@ -46,14 +68,32 @@ function execute(){ }END{ print "[" json "]"; }') - local disks=$(lsblk -b -n -o name,size,fsavail,mountpoint|grep -E '└─|├─'|awk '{ + local swaps=$(cat /proc/swaps|grep /dev/|awk '{ + gsub(/\/dev\//, "", $1); + results = $1 ":" ($3 - $4) * 1024; + swaps = swaps != "" ? swaps "," results : results; + }END{ + print swaps; + }') + local disks=$(lsblk -b -n -o name,kname,size,fsavail,mountpoint|grep -E '└─|├─'|awk -v swap_map="$swaps" 'BEGIN{ + l = split(swap_map, free, ","); + for(i = 1; i <= l; i++){ + split(free[i], item, ":"); + swaps[item[1]] = item[2]; + } + }{ + gsub(/├─|└─/, "", $1); device = $1; - total = $2; - available = $3 ~ /^\// || $3 == "" ? 0 : $3; - mountpoint = $4 != "" ? "\"" $4 "\"" : $3 ~ /^\// ? "null" : "\"" $3 "\""; + total = $3; + if($5 == "[SWAP]" || $4 == "[SWAP]"){ + available = swaps[$2] != "" ? swaps[$2] : 0; + mountpoint = "null"; + }else{ + available = $4 ~ /^\// || $4 == "" ? 0 : $4; + mountpoint = $5 != "" ? "\"" $5 "\"" : $4 ~ /^\// ? "null" : "\"" $4 "\""; + } - gsub(/├─|└─/, "", device); array = "[\"" device "\"," total "," available "," mountpoint "]"; json = json == "" ? array : json "," array; @@ -62,6 +102,8 @@ function execute(){ print "[" json "]"; }') local iterations=0 + local cpu_total= + local cpu_idle= local cpu_in= local cpu_out= local cpu_minimum= @@ -80,48 +122,49 @@ function execute(){ local candle_end= net_data=$net_data$(get_net_data) + + read -r cpu_total cpu_idle <<< $(get_cpu_data) while true; do - cpu=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}') - # memory=$(cat /proc/meminfo|grep MemFree:|awk '{print $2 * 1024}') memory=$(cat /proc/meminfo|grep MemAvailable:|awk '{print $2 * 1024}') - # cpu_average=$((cpu_average + cpu)) - # memory_average=$((memory_average + memory)) - cpu_average=$(echo "$cpu_average + ${cpu/,/.}"|bc -l) - memory_average=$(echo "$memory_average + $memory"|bc -l) + memory_average=$(echo "$memory_average $memory"|awk '{print $1 + $2}') - cpu=${cpu/,/.} - - cpu_out=$cpu memory_out=$memory iterations=$((iterations + 1)) - if [ -z "$cpu_in" ]; then - cpu_in=$cpu + if [ -z "$memory_in" ]; then memory_in=$memory - cpu_minimum=$cpu - cpu_maximum=$cpu memory_minimum=$memory memory_maximum=$memory else - if [ "$(echo "$cpu < $cpu_minimum"|bc -l)" -eq 1 ];then cpu_minimum=$cpu;fi - if [ "$(echo "$cpu > $cpu_maximum"|bc -l)" -eq 1 ];then cpu_maximum=$cpu;fi - if [ "$(echo "$memory < $memory_minimum"|bc -l)" -eq 1 ];then memory_minimum=$memory;fi - if [ "$(echo "$memory > $memory_maximum"|bc -l)" -eq 1 ];then memory_maximum=$memory;fi - # cpu_minimum=$(echo -e "$cpu_minimum\n$cpu" | sort -n | head -1) - # cpu_maximum=$(echo -e "$cpu_maximum\n$cpu" | sort -n | tail -1) - # memory_minimum=$(echo -e "$memory_minimum\n$memory" | sort -n | head -1) - # memory_maximum=$(echo -e "$memory_maximum\n$memory" | sort -n | tail -1) + awk -v t="$memory" -v m="$memory_minimum" 'BEGIN{exit !(t < m)}' && memory_minimum=$memory + awk -v t="$memory" -v m="$memory_maximum" 'BEGIN{exit !(t > m)}' && memory_maximum=$memory if [ $(date +%s) -ge $limit_seconds ]; then break fi - sleep 1 fi + + sleep $candle_sleep_seconds + + read -r cpu cpu_total cpu_idle <<< $(get_cpu_value $cpu_total $cpu_idle) + + cpu_average=$(echo "$cpu_average $cpu"|awk '{print $1 + $2}') + cpu_out=$cpu + + if [ -z "$cpu_in" ]; then + cpu_in=$cpu + cpu_minimum=$cpu + cpu_maximum=$cpu + else + awk -v t="$cpu" -v m="$cpu_minimum" 'BEGIN{exit !(t < m)}' && cpu_minimum=$cpu + awk -v t="$cpu" -v m="$cpu_maximum" 'BEGIN{exit !(t > m)}' && cpu_maximum=$cpu + fi + done if [ -z "$domain" ]; then @@ -130,18 +173,16 @@ function execute(){ domain="\"$domain\"" fi - # cpu_average=$((cpu_average / iterations)) - # memory_average=$((memory_average / iterations)) - cpu_average=$(echo "scale=6; $cpu_average / $iterations"|bc|awk '{printf $1 + 0}') - memory_average=$(echo "scale=6; $memory_average / $iterations"|bc|awk '{printf $1 + 0}') + cpu_average=$(echo "$cpu_average $iterations"|awk '{printf "%.6f", $1 / $2}') + memory_average=$(echo "$memory_average $iterations"|awk '{printf "%.6f", $1 / $2}') net_data="$net_data,$(get_net_data)]" candle_end=$(date +%s) json="$json$hostnames,$domain,$ips,$disks,$iterations" json="$json,[$candle_start,$candle_end]" - json="$json,[${cpu_in//,/.},${cpu_out//,/.},${cpu_minimum//,/.},${cpu_maximum//,/.},$cpu_average]" - json="$json,[$memory_total,$memory_in,$memory_out,$memory_minimum,$memory_maximum,$memory_average]" + json="$json,[${cpu_in//,/.},${cpu_out//,/.},${cpu_minimum//,/.},${cpu_maximum//,/.},${cpu_average//,/.}]" + json="$json,[$memory_total,$memory_in,$memory_out,$memory_minimum,$memory_maximum,${memory_average//,/.}]" json="$json,$net_data" json="$json]" diff --git a/SQLServer/NucelarMonitor.server.01.remove.server.sql b/SQLServer/NucelarMonitor.server.01.remove.server.sql new file mode 100644 index 0000000..8dd8e84 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.01.remove.server.sql @@ -0,0 +1,45 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.tables_drop', N'P') is not null drop procedure dbo.tables_drop +go +create procedure dbo.tables_drop as begin + + set nocount on + + -- Level Plains. + if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is not null drop table dbo.MachineInterfacesTrafficPlain + if object_id(N'dbo.MachineInterfacesPlain', N'U') is not null drop table dbo.MachineInterfacesPlain + if object_id(N'dbo.MachineDisksPlain', N'U') is not null drop table dbo.MachineDisksPlain + if object_id(N'dbo.MachinePlain', N'U') is not null drop table dbo.MachinePlain + + -- Level 2. + if object_id(N'dbo.MachineInterfacesData', N'U') is not null drop table dbo.MachineInterfacesData + if object_id(N'dbo.MachineInterfacesTraffic', N'U') is not null drop table dbo.MachineInterfacesTraffic + if object_id(N'dbo.MachineDisksSpace', N'U') is not null drop table dbo.MachineDisksSpace + if object_id(N'dbo.Exceptions', N'U') is not null drop table dbo.Exceptions + + -- Level 1. + if object_id(N'dbo.MachineInterfaces', N'U') is not null drop table dbo.MachineInterfaces + if object_id(N'dbo.MachineDisks', N'U') is not null drop table dbo.MachineDisks + if object_id(N'dbo.MachineRAM', N'U') is not null drop table dbo.MachineRAM + if object_id(N'dbo.MachineCPU', N'U') is not null drop table dbo.MachineCPU + if object_id(N'dbo.Procedures', N'U') is not null drop table dbo.Procedures + + -- Level 0. + if object_id(N'dbo.CandlesTimes', N'U') is not null drop table dbo.CandlesTimes + if object_id(N'dbo.Types', N'U') is not null drop table dbo.Types + if object_id(N'dbo.Interfaces', N'U') is not null drop table dbo.Interfaces + if object_id(N'dbo.Disks', N'U') is not null drop table dbo.Disks + if object_id(N'dbo.Hostnames', N'U') is not null drop table dbo.Hostnames + if object_id(N'dbo.Domains', N'U') is not null drop table dbo.Domains + if object_id(N'dbo.Machines', N'U') is not null drop table dbo.Machines + if object_id(N'dbo.Databases', N'U') is not null drop table dbo.Databases + if object_id(N'dbo.Messages', N'U') is not null drop table dbo.Messages + if object_id(N'dbo.BigData', N'U') is not null drop table dbo.BigData + +end +go + +-- execute dbo.tables_drop \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.02.create.server.sql b/SQLServer/NucelarMonitor.server.02.create.server.sql new file mode 100644 index 0000000..3c139d7 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.02.create.server.sql @@ -0,0 +1,560 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.tables_create', N'P') is not null drop procedure dbo.tables_create +go +create procedure dbo.tables_create as begin + + set nocount on + + -- Level 0. + if object_id(N'dbo.Machines', N'U') is null create table dbo.Machines( + id integer not null identity(1, 1), + [key] varchar(32) not null, + [description] varchar(512), + date_in datetime not null constraint machines_df_date_in default getdate(), + date_out datetime, + constraint machines_pk primary key clustered (id), + constraint machines_uk_key unique ([key]), + constraint machines_ck_key check ([key] like '[a-zA-Z0-9][a-zA-Z0-9_]{0,31}') + ) + + if object_id(N'dbo.Types', N'U') is null create table dbo.Types( + id integer not null identity(1, 1), + [name] varchar(32) not null, + [description] varchar(512), + date_in datetime not null constraint types_df_date_in default getdate(), + date_out datetime, + constraint types_pk primary key clustered (id), + constraint types_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint types_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,31}') + ) + + if object_id(N'dbo.Domains', N'U') is null create table dbo.Domains( + id integer not null identity(1, 1), + domain varchar(64) not null, + [description] varchar(512), + date_in datetime not null constraint domains_df_date_in default getdate(), + date_out datetime, + constraint domains_pk primary key clustered (id), + constraint domains_uk_domain unique nonclustered (domain asc) with (fillfactor = 90), + constraint domains_ck_domain check ([domain] like '[a-zA-Z0-9_-.]{1,64}') + ) + + + if object_id(N'dbo.Hostnames', N'U') is null create table dbo.Hostnames( + id integer not null identity(1, 1), + [name] varchar(32) not null, + [description] varchar(512), + date_in datetime not null constraint hostnames_df_date_in default getdate(), + date_out datetime, + constraint hostnames_pk primary key clustered (id), + constraint hostnames_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint hostnames_ck_name check ([name] like '[a-zA-Z0-9_-]{1,32}') + ) + + if object_id(N'dbo.Disks', N'U') is null create table dbo.Disks( + id integer not null identity(1, 1), + [name] varchar(32) not null, + [size] bigint not null, + mountpoint varchar(128) not null, + [description] varchar(512), + date_in datetime not null constraint disks_df_date_in default getdate(), + date_out datetime, + constraint disks_pk primary key clustered (id), + -- constraint disks_uk_name unique nonclustered ([name] asc, mountpoint asc) with (fillfactor = 90), + constraint disks_ck_name check ( + [name] like '[a-zA-Z0-9_-]{1,32}' or + mountpoint like '[A-Z]:' + ), + constraint disks_ck_size check (size >= 0 and size < power(2, 53)), + constraint disks_ck_mountpoint check ( + mountpoint like '/%' or + mountpoint like '[a-zA-Z]:\%' or + mountpoint like '\\%' + ) + ) + + if object_id(N'dbo.Interfaces', N'U') is null create table dbo.Interfaces( + id integer not null identity(1, 1), + [name] varchar(32) not null, + [description] varchar(512), + date_in datetime not null constraint interfaces_df_date_in default getdate(), + date_out datetime, + constraint interfaces_pk primary key clustered (id), + -- constraint interfaces_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint interfaces_ck_name check ([name] like '[a-zA-Z0-9_-]{1,32}') + ) + + if object_id(N'dbo.CandlesTimes', N'U') is null create table dbo.CandlesTimes( + id integer not null identity(1, 1), + [from] datetime not null, + [to] datetime not null, + iterations integer not null, + date_in datetime not null constraint candles_times_df_date_in default getdate(), + date_out datetime, + constraint candles_times_pk primary key clustered (id), + constraint candles_times_uk_from_to unique nonclustered ([from] asc, [to] asc) with (fillfactor = 90), + constraint candles_times_ck_from_to check ([from] < [to]) + ) + + if object_id(N'dbo.BigData', N'U') is null create table dbo.BigData( + id integer not null identity(1, 1), + [hash] binary(64) not null, + [value] varchar(max) not null, + date_in datetime not null constraint big_data_df_date_in default getdate(), + date_out datetime, + constraint big_data_pk primary key clustered (id), + constraint big_data_uk_hash unique nonclustered ([hash] asc) with (fillfactor = 90) + ) + + if object_id(N'dbo.Messages', N'U') is null create table dbo.Messages( + id integer not null identity(1, 1), + [key] varchar(128) not null, + date_in datetime not null constraint messages_df_date_in default getdate(), + date_out datetime, + constraint messages_pk primary key clustered (id), + constraint messages_ck_key check ([key] like '[a-zA-Z_][a-zA-Z0-9_]{0,127}') + ) + + if object_id(N'dbo.Databases', N'U') is null create table dbo.Databases( + id integer not null identity(1, 1), + [name] varchar(64) not null, + date_in datetime not null constraint databases_df_date_in default getdate(), + date_out datetime, + constraint databases_pk primary key clustered (id), + constraint databases_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint databases_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,63}') + ) + + -- Level 1. + if object_id(N'dbo.MachineCPU', N'U') is null create table dbo.MachineCPU( + id integer not null identity(1, 1), + machine integer not null, + candle_time integer not null, + [in] float not null, + [out] float not null, + minimum float not null, + maximum float not null, + average float not null, + date_in datetime not null constraint machine_cpu_df_date_in default getdate(), + date_out datetime, + constraint machine_cpu_pk primary key clustered (id), + constraint machine_cpu_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_cpu_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_cpu_ck_cpu_in check ( + [in] between 0 and 100 and + [in] between minimum and maximum + ), + constraint machine_cpu_ck_cpu_out check ( + [out] between 0 and 100 and + [out] between minimum and maximum + ), + constraint machine_cpu_ck_cpu_minimum check ( + minimum between 0 and 100 and + minimum <= maximum + ), + constraint machine_cpu_ck_cpu_maximum check ( + maximum between 0 and 100 and + maximum >= minimum + ), + constraint machine_cpu_ck_cpu_average check ( + average between 0 and 100 and + average between minimum and maximum + ) + ) + + if object_id(N'dbo.MachineRAM', N'U') is null create table dbo.MachineRAM( + id integer not null identity(1, 1), + machine integer not null, + candle_time integer not null, + total bigint not null, + [in] bigint not null, + [out] bigint not null, + minimum bigint not null, + maximum bigint not null, + average bigint not null, + date_in datetime not null constraint machine_ram_df_date_in default getdate(), + date_out datetime, + constraint machine_ram_pk primary key clustered (id), + constraint machine_ram_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_ram_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_ram_ck_memory_total check ( + total between 0 and power(2, 53) - 1 + ), + constraint machine_ram_ck_memory_in check ( + [in] between 0 and power(2, 53) - 1 and + [in] between minimum and maximum and + [in] <= total + ), + constraint machine_ram_ck_memory_out check ( + [out] between 0 and power(2, 53) - 1 and + [out] between minimum and maximum and + [out] <= total + ), + constraint machine_ram_ck_memory_minimum check ( + minimum between 0 and power(2, 53) - 1 and + minimum <= total and + minimum <= maximum + ), + constraint machine_ram_ck_memory_maximum check ( + maximum between 0 and power(2, 53) - 1 and + maximum between total and minimum + ), + constraint machine_ram_ck_memory_average check ( + average between 0 and power(2, 53) - 1 and + average between minimum and maximum and + average <= total + ) + ) + + if object_id(N'dbo.MachineDisks', N'U') is null create table dbo.MachineDisks( + id integer not null identity(1, 1), + machine integer not null, + [disk] integer not null, + belongs bit not null constraint machine_disks_df_belongs default 1, + mounted bit not null constraint machine_disks_df_mounted default 1, + date_in datetime not null constraint machine_disks_df_date_in default getdate(), + date_out datetime, + constraint machine_disks_pk primary key clustered (id), + constraint machine_disks_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_disks_fk_disk foreign key ([disk]) references dbo.Disks(id) + on update no action + on delete no action + ) + + if object_id(N'dbo.MachineInterfaces', N'U') is null create table dbo.MachineInterfaces( + id integer not null identity(1, 1), + machine integer not null, + [interface] integer not null, + belongs bit not null constraint machine_interfaces_df_belongs default 1, + mounted bit not null constraint machine_interfaces_df_mounted default 1, + date_in datetime not null constraint machine_interfaces_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_pk primary key clustered (id), + constraint machine_interfaces_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_interfaces_fk_interface foreign key ([interface]) references dbo.Interfaces(id) + on update no action + on delete no action + ) + + if object_id(N'dbo.Procedures', N'U') is null create table dbo.Procedures( + id integer not null identity(1, 1), + [database] integer not null, + [name] varchar(64) not null, + date_in datetime not null constraint procedures_df_date_in default getdate(), + date_out datetime, + constraint procedures_pk primary key clustered (id), + constraint procedures_fk_database foreign key ([database]) references dbo.Databases(id) + on update no action + on delete no action, + constraint procedures_uk_name unique nonclustered ([database] asc, [name] asc) with (fillfactor = 90), + constraint procedures_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,63}') + ) + + -- Level 2. + if object_id(N'dbo.MachineDisksSpace', N'U') is null create table dbo.MachineDisksSpace( + id integer not null identity(1, 1), + machine_disk integer not null, + candle_time integer not null, + free bigint not null, + date_in datetime not null constraint machine_disks_space_df_date_in default getdate(), + date_out datetime, + constraint machine_disks_space_pk primary key clustered (id), + constraint machine_disks_space_fk_machine_disk foreign key (machine_disk) references dbo.MachineDisks(id) + on update no action + on delete no action, + constraint machine_disks_space_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_disks_space_ck_free check ( + free between 0 and power(2, 53) - 1 -- and + -- free <= isnull(( + -- select top 1 disks.[size] + -- from dbo.Disks disks + -- join dbo.MachineDisks machine_disks on machine_disks.[disk] = disks.id + -- where + -- disks.date_out is null and + -- machine_disks.date_out is null and + -- machine_disks.belongs = 1 and + -- machine_disks.id = machine_disk + -- ), 0) + ) + ) + + if object_id(N'dbo.MachineInterfacesTraffic', N'U') is null create table dbo.MachineInterfacesTraffic( + id integer not null identity(1, 1), + machine_interface integer not null, + candle_time integer not null, + [type] integer not null, + bytes bigint not null, + packages integer not null, + errors integer not null, + date_in datetime not null constraint machine_interfaces_traffic_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_traffic_pk primary key clustered (id), + constraint machine_interfaces_traffic_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_fk_type foreign key ([type]) references dbo.Types(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_ck_bytes check ( + bytes between 0 and power(2, 53) - 1 + ), + constraint machine_interfaces_traffic_ck_packages check ( + packages between 0 and power(2, 31) - 1 + ), + constraint machine_interfaces_traffic_ck_errors check ( + errors between 0 and power(2, 31) - 1 + ) + ) + + if object_id(N'dbo.MachineInterfacesData', N'U') is null create table dbo.MachineInterfacesData( + id integer not null identity(1, 1), + machine_interface integer not null, + is_ipv6 bit not null constraint machine_interfaces_data_df_is_ipv6 default 0, + [address] varchar(45) not null, + mask tinyint not null, + date_in datetime not null constraint machine_interfaces_data_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_data_pk primary key clustered (id), + constraint machine_interfaces_data_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) + on update no action + on delete no action, + constraint machine_interfaces_data_ck_is_ipv6 check ( + (is_ipv6 = 0 and [address] like '[0-9]%.%.%.%') or + (is_ipv6 = 1 and [address] like '%:%') + ), + constraint machine_interfaces_data_ck_address check ( + ([address] like '[0-9]%.%.%.%' and mask between 0 and 32) or + ([address] like '%:%' and mask between 0 and 128) + ), + constraint machine_interfaces_data_ck_mask check ( + (mask between 0 and (case when is_ipv6 = 1 then 128 else 32 end)) + ) + ) + + if object_id(N'dbo.Exceptions', N'U') is null create table dbo.Exceptions( + id integer not null identity(1, 1), + [procedure] integer not null, + [message] integer not null, + parameters integer, + exception integer not null, + [status] varchar(16), + code integer, + date_in datetime not null constraint exceptions_df_date_in default getdate(), + date_out datetime, + constraint exceptions_pk primary key clustered (id), + constraint exceptions_fk_procedure foreign key ([procedure]) references dbo.Procedures(id) + on update no action + on delete no action, + constraint exceptions_fk_message foreign key ([message]) references dbo.Messages(id) + on update no action + on delete no action, + constraint exceptions_fk_parameters foreign key (parameters) references dbo.BigData(id) + on update no action + on delete no action, + constraint exceptions_fk_exception foreign key (exception) references dbo.BigData(id) + on update no action + on delete no action + ) + + -- Level Plains 1. + if object_id(N'dbo.MachinePlain', N'U') is null create table dbo.MachinePlain( + id integer not null identity(1, 1), + [key] varchar(32) not null, + machine integer not null, + candle_time integer not null, + machine_ram integer not null, + machine_cpu integer not null, + ram_total bigint not null, + ram_in bigint not null, + ram_out bigint not null, + ram_minimum bigint not null, + ram_maximum bigint not null, + ram_average bigint not null, + cpu_in float not null, + cpu_out float not null, + cpu_minimum float not null, + cpu_maximum float not null, + cpu_average float not null, + date_in datetime not null constraint machine_plain_df_date_in default getdate(), + date_out datetime, + constraint machine_plain_pk primary key clustered (id), + constraint machine_plain_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_plain_fk_machine_ram foreign key (machine_ram) references dbo.MachineRAM(id) + on update no action + on delete no action, + constraint machine_plain_fk_machine_cpu foreign key (machine_cpu) references dbo.MachineCPU(id) + on update no action + on delete no action, + constraint machine_plain_uk_key unique ([key]), + constraint machine_plain_ck_key check ([key] like '[a-zA-Z0-9][a-zA-Z0-9_]{0,31}'), + constraint machine_plain_ck_memory_total check ( + ram_total between 0 and power(2, 53) - 1 + ), + constraint machine_plain_ck_memory_in check ( + ram_in between 0 and power(2, 53) - 1 and + ram_in between ram_minimum and ram_maximum and + ram_in <= ram_total + ), + constraint machine_plain_ck_memory_out check ( + ram_out between 0 and power(2, 53) - 1 and + ram_out between ram_minimum and ram_maximum and + ram_out <= ram_total + ), + constraint machine_plain_ck_memory_minimum check ( + ram_minimum between 0 and power(2, 53) - 1 and + ram_minimum <= ram_total and + ram_minimum <= ram_maximum + ), + constraint machine_plain_ck_memory_maximum check ( + ram_maximum between 0 and power(2, 53) - 1 and + ram_maximum between ram_total and ram_minimum + ), + constraint machine_plain_ck_memory_average check ( + ram_average between 0 and power(2, 53) - 1 and + ram_average between ram_minimum and ram_maximum and + ram_average <= ram_total + ), + constraint machine_plain_ck_cpu_in check ( + cpu_in between 0 and 100 and + cpu_in between cpu_minimum and cpu_maximum + ), + constraint machine_plain_ck_cpu_out check ( + cpu_out between 0 and 100 and + cpu_out between cpu_minimum and cpu_maximum + ), + constraint machine_plain_ck_cpu_minimum check ( + cpu_minimum between 0 and 100 and + cpu_minimum <= cpu_maximum + ), + constraint machine_plain_ck_cpu_maximum check ( + cpu_maximum between 0 and 100 and + cpu_maximum >= cpu_minimum + ), + constraint machine_plain_ck_cpu_average check ( + cpu_average between 0 and 100 and + cpu_average between cpu_minimum and cpu_maximum + ) + ) + + -- Level Plains 1. + if object_id(N'dbo.MachineInterfacesPlain', N'U') is null create table dbo.MachineInterfacesPlain( + id integer not null identity(1, 1), + machine integer not null, + interface integer not null, + machine_interface integer not null, + [name] varchar(32) not null, + is_ipv6 bit not null constraint machine_interfaces_plain_df_is_ipv6 default 0, + [address] varchar(45) not null, + mask tinyint not null, + belongs bit not null constraint machine_interfaces_plain_df_belongs default 1, + date_in datetime not null constraint machine_interfaces_plain_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_plain_pk primary key clustered (id), + constraint machine_interfaces_plain_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_interfaces_plain_fk_interface foreign key (interface) references dbo.Interfaces(id) + on update no action + on delete no action, + constraint machine_interfaces_plain_uk_machine_interface unique nonclustered (machine asc, [interface] asc) with (fillfactor = 90), + -- constraint machine_interfaces_plain_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint machine_interfaces_plain_ck_name check ([name] like '[a-zA-Z0-9_-]{1,32}'), + constraint machine_interfaces_plain_ck_interface check ([interface] like '[a-zA-Z0-9_-]{1,32}') + ) + + if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is null create table dbo.MachineInterfacesTrafficPlain( + id integer not null identity(1, 1), + machine_interface integer not null, + candle_time integer not null, + [type] integer not null, + bytes bigint not null, + packages integer not null, + errors integer not null, + date_in datetime not null constraint machine_interfaces_traffic_plain_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_traffic_plain_pk primary key clustered (id), + constraint machine_interfaces_traffic_plain_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfacesPlain(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_fk_type foreign key ([type]) references dbo.Types(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_ck_bytes check (bytes between 0 and power(2, 53) - 1), + constraint machine_interfaces_traffic_plain_ck_packages check (packages between 0 and power(2, 31) - 1), + constraint machine_interfaces_traffic_plain_ck_errors check (errors between 0 and power(2, 31) - 1) + ) + + if object_id(N'dbo.MachineDisksPlain', N'U') is null create table dbo.MachineDisksPlain( + id integer not null identity(1, 1), + plain integer not null, + machine integer not null, + [disk] integer not null, + machine_disk integer not null, + candle_time integer not null, + [name] varchar(32) not null, + [size] bigint not null, + mountpoint varchar(128) not null, + free bigint not null, + belongs bit not null constraint machine_disks_plain_df_belongs default 1, + date_in datetime not null constraint machine_disks_plain_df_date_in default getdate(), + date_out datetime, + constraint machine_disks_plain_pk primary key clustered (id), + constraint machine_disks_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) + on update no action + on delete no action, + constraint machine_disks_plain_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action, + constraint machine_disks_plain_fk_disk foreign key ([disk]) references dbo.Disks(id) + on update no action + on delete no action, + constraint machine_disks_plain_uk_machine_disk unique nonclustered (machine asc, [disk] asc) with (fillfactor = 90), + -- constraint machine_disks_plain_uk_name unique nonclustered ([name] asc, mountpoint asc) with (fillfactor = 90), + constraint machine_disks_plain_ck_name check ( + [name] like '[a-zA-Z0-9_-]{1,32}' or + mountpoint like '[A-Z]:' + ), + constraint machine_disks_plain_ck_size check (size >= 0 and size < power(2, 53)), + constraint machine_disks_plain_ck_mountpoint check ( + mountpoint like '/%' or + mountpoint like '[a-zA-Z]:\%' or + mountpoint like '\\%' + ), + constraint machine_disks_plain_ck_free check ( + free between 0 and power(2, 53) - 1 and + free <= [size] + ) + ) + +end +go + +execute dbo.tables_create \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.03.update.server.sql b/SQLServer/NucelarMonitor.server.03.update.server.sql new file mode 100644 index 0000000..240cdf0 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.03.update.server.sql @@ -0,0 +1,14 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.tables_update', N'P') is not null drop procedure dbo.tables_update +go +create procedure dbo.tables_update as begin + + set nocount on + +end +go + +execute dbo.tables_update \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.04.fill.server.sql b/SQLServer/NucelarMonitor.server.04.fill.server.sql new file mode 100644 index 0000000..59cfb23 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.04.fill.server.sql @@ -0,0 +1,14 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.tables_fill', N'P') is not null drop procedure dbo.tables_fill +go +create procedure dbo.tables_fill as begin + + set nocount on + +end +go + +execute dbo.tables_fill \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.05.common.server.sql b/SQLServer/NucelarMonitor.server.05.common.server.sql new file mode 100644 index 0000000..012dfd9 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.05.common.server.sql @@ -0,0 +1,10 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.hash_big_data', N'FN') is not null drop function dbo.hash_big_data +go +create function dbo.hash_big_data (@value varchar(max)) returns binary(64) begin + return (select top 1 convert(binary(64), hashbytes('SHA2_256', convert(nvarchar(max), @value)))) +end +go \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.06.views.server.sql b/SQLServer/NucelarMonitor.server.06.views.server.sql new file mode 100644 index 0000000..97e138e --- /dev/null +++ b/SQLServer/NucelarMonitor.server.06.views.server.sql @@ -0,0 +1,43 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.ProceduresView', N'V') is not null drop view dbo.ProceduresView +go +create view dbo.ProceduresView as select + procedures.id, + databases.[name] as [database], + procedures.[name] as [procedure], + procedures.date_in, + procedures.date_out +from dbo.Procedures procedures +join dbo.Databases databases on databases.id = procedures.[database] +where + procedures.date_out is null and + databases.date_out is null +go + +if object_id(N'dbo.ExceptionsView', N'V') is not null drop view dbo.ExceptionsView +go +create view dbo.ExceptionsView as select + exceptions.id, + procedures.[database] as [database], + procedures.[procedure] as [procedure], + messages.[key] as [message], + parameters.[value] as [parameters], + exception.[value] as [exception], + exceptions.[status], + exceptions.code, + exceptions.date_in +from dbo.Exceptions exceptions +join dbo.ProceduresView procedures on procedures.id = exceptions.[procedure] +join dbo.Messages messages on messages.id = exceptions.[message] +join dbo.BigData parameters on parameters.id = exceptions.parameters +join dbo.BigData exception on exception.id = exceptions.exception +where + exceptions.date_out is null and + procedures.date_out is null and + messages.date_out is null and + parameters.date_out is null and + exception.date_out is null +go \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.07.getters.server.sql b/SQLServer/NucelarMonitor.server.07.getters.server.sql new file mode 100644 index 0000000..0e62ed3 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.07.getters.server.sql @@ -0,0 +1,204 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.get_procedure', N'P') is not null drop procedure dbo.get_procedure +go +create procedure dbo.get_procedure + @database varchar(64), + @procedure varchar(64), + @id integer output +as begin + + declare @database_id integer = ( + select top 1 id from dbo.Databases where + date_out is null and + [name] = @database + ) + + set nocount on + + if @database_id is null begin + insert into dbo.Databases([name]) values (@database) + set @database_id = scope_identity() + end + + set @id = ( + select top 1 id from dbo.Procedures where + date_out is null and + [database] = @database_id and + [name] = @procedure + ) + + if @id is null begin + insert into dbo.Procedures([database], [name]) values (@database_id, @procedure) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_big_data', N'P') is not null drop procedure dbo.get_big_data +go +create procedure dbo.get_big_data + @value varchar(max), + @id integer output +as begin + + declare @hash binary(64) = dbo.hash_big_data(@value) + + set nocount on + + set @id = ( + select top 1 id from dbo.BigData where + date_out is null and + [hash] = @hash + ) + + if @id is null begin + insert into dbo.BigData([hash], [value]) values (@hash, @value) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_message', N'P') is not null drop procedure dbo.get_message +go +create procedure dbo.get_message + @message varchar(128), + @id integer output +as begin + + set nocount on + + set @id = ( + select top 1 id from dbo.Messages where + date_out is null and + [key] = @message + ) + + if @id is null begin + insert into dbo.Messages([key]) values (@message) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_type', N'P') is not null drop procedure dbo.get_type +go +create procedure dbo.get_type + @name varchar(32), + @id integer output +as begin set nocount on + + set @id = ( + select top 1 id from dbo.Types where + date_out is null and + [name] = @name + ) + + if @id is null begin + insert into dbo.Types([name]) values (@name) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_machine', N'P') is not null drop procedure dbo.get_machine +go +create procedure dbo.get_machine + @key varchar(32), + @id integer output +as begin set nocount on + + set @id = ( + select top 1 id from dbo.Machines where + date_out is null and + [key] = @key + ) + + if @id is null begin + insert into dbo.Machines([key]) values (@key) + set @id = scope_identity() + end + +end + +if object_id(N'dbo.get_basic_data_ids', N'P') is not null drop procedure dbo.get_basic_data_ids +go +create procedure dbo.get_basic_data_ids + @key varchar(32), + @candle_start datetime, + @candle_end datetime, + @candle_interations integer, + @machine_id integer output, + @candle_time_id integer output +as begin set nocount on + + execute dbo.get_machine @key, @machine_id output + + set @candle_time_id = ( + select top 1 id from dbo.CandlesTimes where + date_out is null and + [from] = @candle_start and + [to] = @candle_end and + iterations = @candle_interations + ) + if @candle_time_id is null begin + insert into dbo.CandlesTimes([from], [to], iterations) values (@candle_start, @candle_end, @candle_interations) + set @candle_time_id = scope_identity() + end + set @candle_time_id = @candle_time_id + +end +go + +if object_id(N'dbo.get_interface', N'P') is not null drop procedure dbo.get_interface +go +create procedure dbo.get_interface + @name varchar(32), + @id integer output +as begin set nocount on + + set @id = ( + select top 1 id from dbo.Interfaces where + date_out is null and + [name] = @name + ) + + if @id is null begin + insert into dbo.Interfaces([name]) values (@name) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_machine_interface', N'P') is not null drop procedure dbo.get_machine_interface +go +create procedure dbo.get_machine_interface + @key varchar(32), + @name varchar(32), + @candle_start datetime, + @candle_end datetime, + @candle_interations integer, + @machine_id integer output, + @candle_time_id integer output, + @interface_id integer output +as begin set nocount on + + execute dbo.get_basic_data_ids + @key, + @candle_start, + @candle_end, + @candle_interations, + @machine_id output, + @candle_time_id output + + execute dbo.get_interface @name, @interface_id output + +end +go \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.08.setter.server.sql b/SQLServer/NucelarMonitor.server.08.setter.server.sql new file mode 100644 index 0000000..61213a6 --- /dev/null +++ b/SQLServer/NucelarMonitor.server.08.setter.server.sql @@ -0,0 +1,239 @@ +if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 +go +use NucelarMonitor + +if object_id(N'dbo.set_exception', N'P') is not null drop procedure dbo.set_exception +go +create procedure dbo.set_exception + @database varchar(64), + @procedure varchar(64), + @message varchar(128), + @parameters varchar(max) = null +as begin + + declare @procedure_id integer + declare @message_id integer + declare @exception_message varchar(max) = error_message() + declare @exception_message_id integer + declare @parameters_id integer + + set nocount on + + execute dbo.get_procedure @database, @procedure, @procedure_id output + execute dbo.get_big_data @exception_message, @exception_message_id output + execute dbo.get_big_data @parameters, @parameters_id output + execute dbo.get_message @message, @message_id output + + insert into dbo.Exceptions([procedure], [message], [parameters], exception, [status], code) values + (@procedure_id, @message_id, @parameters_id, @exception_message_id, error_severity(), error_number()) + +end +go + +if object_id(N'dbo.set_machine_data', N'P') is not null drop procedure dbo.set_machine_data +go +create procedure dbo.set_machine_data + @key varchar(32), + @candle_start datetime, + @candle_end datetime, + @candle_interations integer, + @ram_total bigint, + @ram_in bigint, + @ram_out bigint, + @ram_minimum bigint, + @ram_maximum bigint, + @ram_average bigint, + @cpu_in float, + @cpu_out float, + @cpu_minimum float, + @cpu_maximum float, + @cpu_average float +as begin set nocount on + + declare @machine_id integer + declare @candle_time_id integer + declare @machine_ram_id integer + declare @machine_cpu_id integer + + execute dbo.get_basic_data_ids + @key, + @candle_start, + @candle_end, + @candle_interations, + @machine_id output, + @candle_time_id output + + insert into dbo.MachineRAM(machine, candle_time, total, [in], [out], minimum, maximum, average) values + (@machine_id, @candle_time_id, @ram_total, @ram_in, @ram_out, @ram_minimum, @ram_maximum, @ram_average) + set @machine_ram_id = scope_identity() + + insert into dbo.MachineCPU(machine, candle_time, [in], [out], minimum, maximum, average) values + (@machine_id, @candle_time_id, @cpu_in, @cpu_out, @cpu_minimum, @cpu_maximum, @cpu_average) + set @machine_cpu_id = scope_identity() + + if (select top 1 id from dbo.MachinePlain where + date_out is null and + machine = @machine_id and + candle_time = @candle_time_id + ) is null + insert into dbo.MachinePlain([key], machine, candle_time, machine_ram, machine_cpu, ram_total, ram_in, ram_out, ram_minimum, ram_maximum, ram_average, cpu_in, cpu_out, cpu_minimum, cpu_maximum, cpu_average) values + (@key, @machine_id, @candle_time_id, @machine_ram_id, @machine_cpu_id, @ram_total, @ram_in, @ram_out, @ram_minimum, @ram_maximum, @ram_average, @cpu_in, @cpu_out, @cpu_minimum, @cpu_maximum, @cpu_average) + else + update dbo.MachinePlain set + machine_ram = @machine_ram_id, + machine_cpu = @machine_cpu_id, + ram_total = @ram_total, + ram_in = @ram_in, + ram_out = @ram_out, + ram_minimum = @ram_minimum, + ram_maximum = @ram_maximum, + ram_average = @ram_average, + cpu_in = @cpu_in, + cpu_out = @cpu_out, + cpu_minimum = @cpu_minimum, + cpu_maximum = @cpu_maximum, + cpu_average = @cpu_average + where + date_out is null and + [key] = @key and + candle_time = @candle_time_id + +end +go + +if object_id(N'dbo.set_machine_interface_data', N'P') is not null drop procedure dbo.set_machine_interface_data +go +create procedure dbo.set_machine_interface_data + @key varchar(32), + @name varchar(32), + @bytes bigint, + @packages integer, + @errors integer, + @is_ipv6 bit, + @address varchar(45), + @mask tinyint +as begin set nocount on + + declare @machine_id integer + declare @interface_id integer + declare @machine_interface_id integer + declare @machine_interface_data_id integer + + execute dbo.get_interface @name, @interface_id output + execute dbo.get_machine @key, @machine_id output + + set @machine_interface_id = ( + select top 1 id from dbo.MachineInterfaces where + date_out is null and + machine = @machine_id and + [interface] = @interface_id + ) + + if @machine_interface_id is null begin + insert into dbo.MachineInterfaces(machine, [interface]) values (@machine_id, @interface_id) + set @machine_interface_id = scope_identity() + end + + set @machine_interface_data_id = ( + select top 1 id from dbo.MachineInterfacesData where + date_out is null and + machine_interface = @machine_interface_id and + is_ipv6 = @is_ipv6 and + [address] = @address and + mask = @mask + ) + + if @machine_interface_data_id is null begin + + insert into dbo.MachineInterfacesData(machine_interface, is_ipv6, [address], mask) values + (@machine_interface_id, @is_ipv6, @address, @mask) + set @machine_interface_data_id = scope_identity() + + if (select top 1 0 from dbo.MachineInterfacesPlain where + date_out is null and + machine = @machine_id and + [interface] = @interface_id + ) is null + insert into dbo.MachineInterfacesPlain(plain, machine, [interface], machine_interface, [name], is_ipv6, [address], mask) values + (null, @machine_id, @interface_id, @machine_interface_id, @name, @is_ipv6, @address, @mask) + else + update dbo.MachineInterfacesPlain set + machine_interface = @machine_interface_id, + is_ipv6 = @is_ipv6, + [address] = @address, + mask = @mask + where + date_out is null and + machine = @machine_id and + [interface] = @interface_id + + end + +end +go + +if object_id(N'dbo.set_machine_interface_traffic', N'P') is not null drop procedure dbo.set_machine_interface_traffic +go +create procedure dbo.set_machine_interface_traffic + @key varchar(32), + @name varchar(32), + @type varchar(32), + @candle_start datetime, + @candle_end datetime, + @candle_interations integer, + @bytes bigint, + @packages integer, + @errors integer, +as begin set nocount on + + declare @machine_id integer + declare @type_id integer + declare @candle_time_id integer + declare @interface_id integer + declare @machine_interface_id integer + declare @plain_id integer + + execute dbo.get_basic_data_ids + @key, + @candle_start, + @candle_end, + @candle_interations, + @machine_id output, + @candle_time_id output + execute dbo.get_type @type, @type_id output + execute dbo.get_interface @name, @interface_id output + + set @machine_interface_id = ( + select top 1 id from dbo.MachineInterfaces where + date_out is null and + machine = @machine_id and + [interface] = @interface_id + ) + + insert into dbo.MachineInterfacesTraffic(machine_interface, [type], candle_time, bytes, packages, errors) values + (@machine_interface_id, @type_id, @candle_time_id, @bytes, @packages, @errors) + + set @plain_id = (select top 1 id from dbo.MachineInterfacesPlain where + date_out is null and + machine = @machine_id and + [interface] = @interface_id + ) + + if (select top 1 0 from dbo.MachineInterfacesTrafficPlain where + date_out is null and + machine_interface = @machine_interface_id and + [type] = @type_id + ) is null + insert into dbo.MachineInterfacesTrafficPlain(plain, machine_interface, candle_time, [type], bytes, packages, errors) values + (@plain_id, @machine_interface_id, @candle_time_id, @type_id, @bytes, @packages, @errors) + else + update dbo.MachineInterfacesTrafficPlain set + bytes = @bytes, + candle_time = @candle_time_id + where + date_out is null and + machine_interface = @machine_interface_id and + [type] = @type_id + +end +go \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.sql b/SQLServer/NucelarMonitor.server.sql index 2357824..cd983d7 100644 --- a/SQLServer/NucelarMonitor.server.sql +++ b/SQLServer/NucelarMonitor.server.sql @@ -9,6 +9,7 @@ create procedure dbo.tables_drop as begin set nocount on -- Level Plains. + if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is not null drop table dbo.MachineInterfacesTrafficPlain if object_id(N'dbo.MachineInterfacesPlain', N'U') is not null drop table dbo.MachineInterfacesPlain if object_id(N'dbo.MachineDisksPlain', N'U') is not null drop table dbo.MachineDisksPlain if object_id(N'dbo.MachinePlain', N'U') is not null drop table dbo.MachinePlain @@ -28,6 +29,7 @@ create procedure dbo.tables_drop as begin -- Level 0. if object_id(N'dbo.CandlesTimes', N'U') is not null drop table dbo.CandlesTimes + if object_id(N'dbo.Types', N'U') is not null drop table dbo.Types if object_id(N'dbo.Interfaces', N'U') is not null drop table dbo.Interfaces if object_id(N'dbo.Disks', N'U') is not null drop table dbo.Disks if object_id(N'dbo.Hostnames', N'U') is not null drop table dbo.Hostnames @@ -58,6 +60,17 @@ create procedure dbo.tables_create as begin constraint machines_ck_key check ([key] like '[a-zA-Z0-9][a-zA-Z0-9_]{0,31}') ) + if object_id(N'dbo.Types', N'U') is null create table dbo.Types( + id integer not null identity(1, 1), + [name] varchar(32) not null, + [description] varchar(512), + date_in datetime not null constraint types_df_date_in default getdate(), + date_out datetime, + constraint types_pk primary key clustered (id), + constraint types_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint types_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,31}') + ) + if object_id(N'dbo.Domains', N'U') is null create table dbo.Domains( id integer not null identity(1, 1), domain varchar(64) not null, @@ -326,6 +339,7 @@ create procedure dbo.tables_create as begin id integer not null identity(1, 1), machine_interface integer not null, candle_time integer not null, + [type] integer not null, bytes bigint not null, packages integer not null, errors integer not null, @@ -338,6 +352,9 @@ create procedure dbo.tables_create as begin constraint machine_interfaces_traffic_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, + constraint machine_interfaces_traffic_fk_type foreign key ([type]) references dbo.Types(id) + on update no action + on delete no action, constraint machine_interfaces_traffic_ck_bytes check ( bytes between 0 and power(2, 53) - 1 ), @@ -487,15 +504,10 @@ create procedure dbo.tables_create as begin -- Level Plains 1. if object_id(N'dbo.MachineInterfacesPlain', N'U') is null create table dbo.MachineInterfacesPlain( id integer not null identity(1, 1), - plain integer not null, machine integer not null, interface integer not null, machine_interface integer not null, - candle_time integer not null, [name] varchar(32) not null, - bytes bigint not null, - packages integer not null, - errors integer not null, is_ipv6 bit not null constraint machine_interfaces_plain_df_is_ipv6 default 0, [address] varchar(45) not null, mask tinyint not null, @@ -503,9 +515,6 @@ create procedure dbo.tables_create as begin date_in datetime not null constraint machine_interfaces_plain_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_plain_pk primary key clustered (id), - constraint machine_interfaces_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) - on update no action - on delete no action, constraint machine_interfaces_plain_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, @@ -518,6 +527,31 @@ create procedure dbo.tables_create as begin constraint machine_interfaces_plain_ck_interface check ([interface] like '[a-zA-Z0-9_-]{1,32}') ) + if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is null create table dbo.MachineInterfacesTrafficPlain( + id integer not null identity(1, 1), + interface integer not null, + candle_time integer not null, + [type] integer not null, + bytes bigint not null, + packages integer not null, + errors integer not null, + date_in datetime not null constraint machine_interfaces_traffic_plain_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_traffic_plain_pk primary key clustered (id), + constraint machine_interfaces_traffic_plain_fk_interface foreign key (interface) references dbo.MachineInterfacesPlain(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_fk_type foreign key ([type]) references dbo.Types(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_ck_bytes check (bytes between 0 and power(2, 53) - 1), + constraint machine_interfaces_traffic_plain_ck_packages check (packages between 0 and power(2, 31) - 1), + constraint machine_interfaces_traffic_plain_ck_errors check (errors between 0 and power(2, 31) - 1) + ) + if object_id(N'dbo.MachineDisksPlain', N'U') is null create table dbo.MachineDisksPlain( id integer not null identity(1, 1), plain integer not null, @@ -749,6 +783,47 @@ where exception.date_out is null go +if object_id(N'dbo.get_type', N'P') is not null drop procedure dbo.get_type +go +create procedure dbo.get_type + @name varchar(32), + @id integer output +as begin set nocount on + + set @id = ( + select top 1 id from dbo.Types where + date_out is null and + [name] = @name + ) + + if @id is null begin + insert into dbo.Types([name]) values (@name) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_machine', N'P') is not null drop procedure dbo.get_machine +go +create procedure dbo.get_machine + @key varchar(32), + @id integer output +as begin set nocount on + + set @id = ( + select top 1 id from dbo.Machines where + date_out is null and + [key] = @key + ) + + if @id is null begin + insert into dbo.Machines([key]) values (@key) + set @id = scope_identity() + end + +end + if object_id(N'dbo.get_basic_data_ids', N'P') is not null drop procedure dbo.get_basic_data_ids go create procedure dbo.get_basic_data_ids @@ -758,20 +833,9 @@ create procedure dbo.get_basic_data_ids @candle_interations integer, @machine_id integer output, @candle_time_id integer output -as begin +as begin set nocount on - set nocount on - - set @machine_id = ( - select top 1 id from dbo.Machines where - date_out is null and - [key] = @key - ) - if @machine_id is null begin - insert into dbo.BasicData([key]) values (@key) - set @machine_id = scope_identity() - end - set @machine_id = @machine_id + execute dbo.get_machine @key, @machine_id output set @candle_time_id = ( select top 1 id from dbo.CandlesTimes where @@ -807,15 +871,13 @@ create procedure dbo.set_machine_data @cpu_minimum float, @cpu_maximum float, @cpu_average float -as begin +as begin set nocount on declare @machine_id integer declare @candle_time_id integer declare @machine_ram_id integer declare @machine_cpu_id integer - set nocount on - execute dbo.get_basic_data_ids @key, @candle_start, @@ -862,33 +924,39 @@ as begin end go -if object_id(N'dbo.set_machine_interface_data', N'P') is not null drop procedure dbo.set_machine_interface_data +if object_id(N'dbo.get_interface', N'P') is not null drop procedure dbo.get_interface go -create procedure dbo.set_machine_interface_data +create procedure dbo.get_interface + @name varchar(32), + @id integer output +as begin set nocount on + + set @id = ( + select top 1 id from dbo.Interfaces where + date_out is null and + [name] = @name + ) + + if @id is null begin + insert into dbo.Interfaces([name]) values (@name) + set @id = scope_identity() + end + +end +go + +if object_id(N'dbo.get_machine_interface', N'P') is not null drop procedure dbo.get_machine_interface +go +create procedure dbo.get_machine_interface @key varchar(32), + @name varchar(32), @candle_start datetime, @candle_end datetime, - @candle_interations integer, - @name varchar(32), - @bytes bigint, - @packages integer, - @errors integer, - @is_ipv6 bit, - @address varchar(45), - @mask tinyint -as begin - - declare @machine_id integer - declare @candle_time_id integer - declare @interface_id integer = (select top 1 id from dbo.Interfaces where - date_out is null and - [name] = @name - ) - declare @machine_interface_id integer - declare @machine_interface_data_id integer - declare @machine_interface_traffic_id integer - - set nocount on + @candle_interations integer, + @machine_id integer output, + @candle_time_id integer output, + @interface_id integer output +as begin set nocount on execute dbo.get_basic_data_ids @key, @@ -898,10 +966,31 @@ as begin @machine_id output, @candle_time_id output - if @interface_id is null begin - insert into dbo.Interfaces([name]) values (@name) - set @interface_id = scope_identity() - end + execute dbo.get_interface @name, @interface_id output + +end +go + +if object_id(N'dbo.set_machine_interface_data', N'P') is not null drop procedure dbo.set_machine_interface_data +go +create procedure dbo.set_machine_interface_data + @key varchar(32), + @name varchar(32), + @bytes bigint, + @packages integer, + @errors integer, + @is_ipv6 bit, + @address varchar(45), + @mask tinyint +as begin set nocount on + + declare @machine_id integer + declare @interface_id integer + declare @machine_interface_id integer + declare @machine_interface_data_id integer + + execute dbo.get_interface @name, @interface_id output + execute dbo.get_machine @key, @machine_id output set @machine_interface_id = ( select top 1 id from dbo.MachineInterfaces where @@ -948,32 +1037,53 @@ as begin machine_interface = @machine_interface_id end - insert into dbo.MachineInterfacesTraffic(machine_interface, candle_time, bytes, packages, errors) values - (@machine_interface_id, @candle_time_id, @bytes, @packages, @errors) - set @machine_interface_traffic_id = scope_identity() - if (select top 1 id from dbo.MachineInterfacesPlain where date_out is null and machine = @machine_id and - [interface] = @interface_id and - candle_time = @candle_time_id + [interface] = @interface_id ) is null - insert into dbo.MachineInterfacesPlain(plain, machine, [interface], machine_interface, candle_time, [name], bytes, packages, errors, is_ipv6, [address], mask) values - (null, @machine_id, @interface_id, @machine_interface_id, @candle_time_id, @name, @bytes, @packages, @errors, @is_ipv6, @address, @mask) + insert into dbo.MachineInterfacesPlain(plain, machine, [interface], machine_interface, [name], is_ipv6, [address], mask) values + (null, @machine_id, @interface_id, @machine_interface_id, @name, @is_ipv6, @address, @mask) else update dbo.MachineInterfacesPlain set machine_interface = @machine_interface_id, - bytes = @bytes, - packages = @packages, - errors = @errors, is_ipv6 = @is_ipv6, [address] = @address, mask = @mask where date_out is null and machine = @machine_id and - [interface] = @interface_id and - candle_time = @candle_time_id + [interface] = @interface_id end go + +if object_id(N'dbo.set_machine_interface_traffic', N'P') is not null drop procedure dbo.set_machine_interface_traffic +go +create procedure dbo.set_machine_interface_traffic + @key varchar(32), + @name varchar(32), + @type varchar(32), + @candle_start datetime, + @candle_end datetime, + @candle_interations integer, + @bytes bigint, + @packages integer, + @errors integer, +as begin set nocount on + + declare @machine_id integer + declare @type_id integer + declare @candle_time_id integer + + execute dbo.get_basic_data_ids + @key, + @candle_start, + @candle_end, + @candle_interations, + @machine_id output, + @candle_time_id output + execute dbo.get_type @type, @type_id output + +end +go \ No newline at end of file