/*
 * Copyright 2020 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 *
 */

syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";

package com.centreon.engine;

service Engine {
  rpc GetVersion(google.protobuf.Empty) returns (Version) {}
  rpc GetStats(GenericString) returns (Stats) {}
  rpc GetHost(HostIdentifier) returns (EngineHost) {}
  rpc GetContact(ContactIdentifier) returns (EngineContact) {}
  rpc GetService(ServiceIdentifier) returns (EngineService) {}
  rpc GetHostsCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc GetContactsCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc GetServicesCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc GetServiceGroupsCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc GetContactGroupsCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc GetHostGroupsCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc GetServiceDependenciesCount(google.protobuf.Empty)
      returns (GenericValue) {}
  rpc GetHostDependenciesCount(google.protobuf.Empty) returns (GenericValue) {}
  rpc ProcessServiceCheckResult(Check) returns (CommandSuccess) {}
  rpc ProcessHostCheckResult(Check) returns (CommandSuccess) {}
  rpc NewThresholdsFile(ThresholdsFile) returns (CommandSuccess) {}
  rpc AddHostComment(EngineComment) returns (CommandSuccess) {}
  rpc AddServiceComment(EngineComment) returns (CommandSuccess) {}
  rpc DeleteComment(GenericValue) returns (CommandSuccess) {}
  rpc DeleteAllHostComments(HostIdentifier) returns (CommandSuccess) {}
  rpc DeleteAllServiceComments(ServiceIdentifier) returns (CommandSuccess) {}
  rpc RemoveHostAcknowledgement(HostIdentifier) returns (CommandSuccess) {}
  rpc RemoveServiceAcknowledgement(ServiceIdentifier) returns (CommandSuccess) {
  }
  rpc AcknowledgementHostProblem(EngineAcknowledgement)
      returns (CommandSuccess) {}
  rpc AcknowledgementServiceProblem(EngineAcknowledgement)
      returns (CommandSuccess) {}
  rpc DeleteDowntime(GenericValue) returns (CommandSuccess) {}
  rpc DeleteHostDowntimeFull(DowntimeCriterias) returns (CommandSuccess) {}
  rpc DeleteServiceDowntimeFull(DowntimeCriterias) returns (CommandSuccess) {}
  rpc DeleteDowntimeByHostName(DowntimeHostIdentifier)
      returns (CommandSuccess) {}
  rpc DeleteDowntimeByHostGroupName(DowntimeHostGroupIdentifier)
      returns (CommandSuccess) {}
  rpc DeleteDowntimeByStartTimeComment(DowntimeStartTimeIdentifier)
      returns (CommandSuccess) {}
  rpc DelayHostNotification(HostDelayIdentifier) returns (CommandSuccess) {}
  rpc DelayServiceNotification(ServiceDelayIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleHostDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleServiceDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleHostServicesDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleHostGroupHostsDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleHostGroupServicesDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleServiceGroupHostsDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleServiceGroupServicesDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleAndPropagateHostDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleAndPropagateTriggeredHostDowntime(ScheduleDowntimeIdentifier)
      returns (CommandSuccess) {}
  rpc ScheduleHostCheck(HostCheckIdentifier) returns (CommandSuccess) {}
  rpc ScheduleHostServiceCheck(HostCheckIdentifier) returns (CommandSuccess) {}
  rpc ScheduleServiceCheck(ServiceCheckIdentifier) returns (CommandSuccess) {}
  rpc SignalProcess(EngineSignalProcess) returns (CommandSuccess) {}
  rpc ChangeHostObjectIntVar(ChangeObjectInt) returns (CommandSuccess) {}
  rpc ChangeServiceObjectIntVar(ChangeObjectInt) returns (CommandSuccess) {}
  rpc ChangeContactObjectIntVar(ChangeContactObjectInt)
      returns (CommandSuccess) {}
  rpc ChangeHostObjectCharVar(ChangeObjectChar) returns (CommandSuccess) {}
  rpc ChangeServiceObjectCharVar(ChangeObjectChar) returns (CommandSuccess) {}
  rpc ChangeContactObjectCharVar(ChangeContactObjectChar)
      returns (CommandSuccess) {}
  rpc ChangeHostObjectCustomVar(ChangeObjectCustomVar)
      returns (CommandSuccess) {}
  rpc ChangeServiceObjectCustomVar(ChangeObjectCustomVar)
      returns (CommandSuccess) {}
  rpc ChangeContactObjectCustomVar(ChangeObjectCustomVar)
      returns (CommandSuccess) {}
  rpc ShutdownProgram(google.protobuf.Empty) returns (google.protobuf.Empty) {}
  rpc EnableHostAndChildNotifications(HostIdentifier) returns (CommandSuccess) {
  }
  rpc DisableHostAndChildNotifications(HostIdentifier)
      returns (CommandSuccess) {}
  rpc DisableHostNotifications(HostIdentifier) returns (CommandSuccess) {}
  rpc EnableHostNotifications(HostIdentifier) returns (CommandSuccess) {}
  rpc DisableNotifications(google.protobuf.Empty) returns (CommandSuccess) {}
  rpc EnableNotifications(google.protobuf.Empty) returns (CommandSuccess) {}
  rpc DisableServiceNotifications(ServiceIdentifier) returns (CommandSuccess) {}
  rpc EnableServiceNotifications(ServiceIdentifier) returns (CommandSuccess) {}
  rpc ChangeAnomalyDetectionSensitivity(ChangeServiceNumber)
      returns (CommandSuccess) {}

  rpc GetLogInfo(google.protobuf.Empty) returns (LogInfo) {}
  /**
   * @brief Set level of a logger.
   *
   * @param A message with a logger and a level as strings.
   *
   * @return nothing.
   */
  rpc SetLogLevel(LogLevel) returns (google.protobuf.Empty) {}

  /**
   * @brief Set log flush period of all loggers.
   *
   * @param period in second, 0 means flush all the times
   *
   * @return nothing.
   */
  rpc SetLogFlushPeriod(LogFlushPeriod) returns (google.protobuf.Empty) {}
}

message GenericString {
  string str_arg = 1;
}

message GenericValue {
  int32 value = 1;
}

message CommandSuccess {
  bool value = 1;
}

message Check {
  google.protobuf.Timestamp check_time = 1;
  string host_name = 2;
  string svc_desc = 3;
  string output = 4;
  uint32 code = 5;
}

message Version {
  int32 major = 1;
  int32 minor = 2;
  int32 patch = 3;
}

message ServiceTypeStats {
  double min_latency = 1;
  double max_latency = 2;
  double average_latency = 3;
  double min_execution_time = 4;
  double max_execution_time = 5;
  double average_execution_time = 6;
  double min_state_change = 7;
  double max_state_change = 8;
  double average_state_change = 9;
  uint32 checks_last_1min = 10;
  uint32 checks_last_5min = 11;
  uint32 checks_last_15min = 12;
  uint32 checks_last_1hour = 13;
}

message RestartStats {
  google.protobuf.Timestamp apply_start = 1;
  google.protobuf.Duration objects_expansion = 2;
  google.protobuf.Duration objects_difference = 3;
  google.protobuf.Duration apply_config = 4;
  google.protobuf.Duration apply_timeperiods = 5;
  google.protobuf.Duration apply_connectors = 6;
  google.protobuf.Duration apply_commands = 7;
  google.protobuf.Duration apply_contacts = 8;
  google.protobuf.Duration apply_hosts = 9;
  google.protobuf.Duration apply_services = 10;
  google.protobuf.Duration resolve_hosts = 11;
  google.protobuf.Duration resolve_services = 12;
  google.protobuf.Duration apply_host_dependencies = 13;
  google.protobuf.Duration resolve_host_dependencies = 14;
  google.protobuf.Duration apply_service_dependencies = 15;
  google.protobuf.Duration resolve_service_dependencies = 16;
  google.protobuf.Duration apply_host_escalations = 17;
  google.protobuf.Duration resolve_host_escalations = 18;
  google.protobuf.Duration apply_service_escalations = 19;
  google.protobuf.Duration resolve_service_escalations = 20;
  google.protobuf.Duration apply_new_config = 21;
  google.protobuf.Duration apply_scheduler = 22;
  google.protobuf.Duration check_circular_paths = 23;
  google.protobuf.Duration reload_modules = 24;
  google.protobuf.Timestamp apply_end = 25;
}

message ServicesStats {
  uint32 services_count = 1;
  uint32 checked_services = 2;
  uint32 scheduled_services = 3;
  uint32 actively_checked = 4;
  uint32 passively_checked = 5;
  double min_state_change = 6;
  double max_state_change = 7;
  double average_state_change = 8;
  ServiceTypeStats active_services = 9;
  ServiceTypeStats passive_services = 10;
  uint32 ok = 11;
  uint32 warning = 12;
  uint32 critical = 13;
  uint32 unknown = 14;
  uint32 flapping = 15;
  uint32 downtime = 16;
}

message HostTypeStats {
  double min_latency = 1;
  double max_latency = 2;
  double average_latency = 3;
  double min_execution_time = 4;
  double max_execution_time = 5;
  double average_execution_time = 6;
  double min_state_change = 7;
  double max_state_change = 8;
  double average_state_change = 9;
  uint32 checks_last_1min = 10;
  uint32 checks_last_5min = 11;
  uint32 checks_last_15min = 12;
  uint32 checks_last_1hour = 13;
}

message HostsStats {
  uint32 hosts_count = 1;
  uint32 checked_hosts = 2;
  uint32 scheduled_hosts = 3;
  uint32 actively_checked = 4;
  uint32 passively_checked = 5;
  double min_state_change = 6;
  double max_state_change = 7;
  double average_state_change = 8;
  HostTypeStats active_hosts = 9;
  HostTypeStats passive_hosts = 10;
  uint32 up = 11;
  uint32 down = 12;
  uint32 unreachable = 13;
  uint32 flapping = 14;
  uint32 downtime = 15;
}

message ProgramConfiguration {
  uint32 hosts_count = 1;
}

message ProgramStatus {
  uint32 modified_host_attributes = 1;
  uint32 modified_service_attributes = 2;
  uint32 pid = 3;
  google.protobuf.Timestamp program_start = 4;
  google.protobuf.Timestamp last_command_check = 5;
  google.protobuf.Timestamp last_log_rotation = 6;
  bool enable_notifications = 7;
  bool active_service_checks_enabled = 8;
  bool passive_service_checks_enabled = 9;
  bool active_host_checks_enabled = 10;
  bool passive_host_checks_enabled = 11;
  bool enable_event_handlers = 12;
  bool obsess_over_services = 13;
  bool obsess_over_hosts = 14;
  bool check_service_freshness = 15;
  bool check_host_freshness = 16;
  bool enable_flap_detection = 17;
  bool process_performance_data = 18;
  string global_host_event_handler = 19;
  string global_service_event_handler = 20;
  uint64 next_comment_id = 21;
  uint64 next_event_id = 22;
  uint64 next_problem_id = 23;
  uint64 next_notification_id = 24;
  uint32 total_external_command_buffer_slots = 25;
  uint32 used_external_command_buffer_slots = 26;
  uint32 high_external_command_buffer_slots = 27;
  repeated uint32 active_scheduled_host_check_stats = 28;
  repeated uint32 active_ondemand_host_check_stats = 29;
  repeated uint32 passive_host_check_stats = 30;
  repeated uint32 active_scheduled_service_check_stats = 31;
  repeated uint32 active_ondemand_service_check_stats = 32;
  repeated uint32 passive_service_check_stats = 33;
  repeated uint32 cached_host_check_stats = 34;
  repeated uint32 cached_service_check_stats = 35;
  repeated uint32 external_command_stats = 36;
  repeated uint32 parallel_host_check_stats = 37;
  repeated uint32 serial_host_check_stats = 38;
}

message ExtCmdBuffer {
  uint32 used = 1;
  uint32 high = 2;
  uint32 total = 3;
}

message Stats {
  ProgramConfiguration program_configuration = 1;
  ProgramStatus program_status = 2;
  ServicesStats services_stats = 3;
  HostsStats hosts_stats = 4;
  ExtCmdBuffer buffer = 5;
  RestartStats restart_status = 6;
}

message ThresholdsFile {
  string filename = 1;
}

message HostIdentifier {
  oneof identifier {
    string name = 1;
    uint32 id = 2;
  }
}

message EngineHost {
  string name = 1;
  string alias = 2;
  string address = 3;
  string check_period = 4;
  uint32 id = 5;
  enum State {
    UP = 0;
    DOWN = 1;
    UNREACHABLE = 2;
  }
  State current_state = 6;
}

message ContactIdentifier {
  string name = 1;
}

message EngineContact {
  string name = 1;
  string alias = 2;
  string email = 3;
}

message NameIdentifier {
  string host_name = 1;
  string service_name = 2;
}

message IdIdentifier {
  uint32 host_id = 1;
  uint32 service_id = 2;
}

message ServiceIdentifier {
  oneof identifier {
    NameIdentifier names = 1;
    IdIdentifier ids = 2;
  }
}

message EngineService {
  uint32 host_id = 1;
  uint32 service_id = 2;
  string host_name = 3;
  string description = 4;
  string check_period = 5;
  enum State {
    OK = 0;
    WARNING = 1;
    CRITICAL = 2;
    UNKNOWN = 3;
  }
  State current_state = 6;
}

message EngineComment {
  string host_name = 1;
  string svc_desc = 2;
  string user = 3;
  string comment_data = 4;
  bool persistent = 5;
  uint32 entry_time = 6;
}

message HostDelayIdentifier {
  oneof identifier {
    string name = 1;
    uint32 id = 2;
  }
  uint32 delay_time = 3;
}

message ServiceDelayIdentifier {
  oneof identifier {
    NameIdentifier names = 1;
    IdIdentifier ids = 2;
  }
  uint32 delay_time = 3;
}

message DowntimeCriterias {
  string host_name = 1;
  string service_desc = 2;
  google.protobuf.Int64Value start = 3;
  google.protobuf.Int64Value end = 4;
  google.protobuf.BoolValue fixed = 5;
  google.protobuf.UInt32Value triggered_by = 6;
  google.protobuf.Int32Value duration = 7;
  string author = 8;
  string comment_data = 9;
}

message DowntimeHostIdentifier {
  string host_name = 1;
  string service_desc = 2;
  google.protobuf.UInt32Value start = 3;
  string comment_data = 4;
}

message DowntimeStartTimeIdentifier {
  google.protobuf.UInt32Value start = 1;
  string comment_data = 2;
}

message DowntimeHostGroupIdentifier {
  string host_group_name = 1;
  string host_name = 2;
  string service_desc = 3;
  string comment_data = 4;
  google.protobuf.UInt32Value start = 5;
}

message ScheduleDowntimeIdentifier {
  string host_name = 1;
  string host_group_name = 2;
  string service_group_name = 3;
  string service_desc = 4;
  uint64 start = 5;
  uint64 end = 6;
  bool fixed = 7;
  uint32 triggered_by = 8;
  uint32 duration = 9;
  string author = 10;
  string comment_data = 11;
  uint32 entry_time = 12;
}

message HostCheckIdentifier {
  string host_name = 1;
  uint32 delay_time = 2;
  bool force = 3;
}

message ServiceCheckIdentifier {
  string host_name = 1;
  string service_desc = 2;
  uint32 delay_time = 3;
  bool force = 4;
}

message EngineSignalProcess {
  enum Process {
    RESTART = 0;
    SHUTDOWN = 1;
  }
  Process process = 1;
  uint32 scheduled_time = 2;
}

message EngineAcknowledgement {
  string host_name = 1;
  string service_desc = 2;
  string ack_author = 3;
  string ack_data = 4;
  enum Type {
    NORMAL = 0;
    STICKY = 1;
  }
  Type type = 5;
  bool notify = 6;
  bool persistent = 7;
}

/* used for a host or a service */
message ChangeObjectInt {
  string host_name = 1;
  string service_desc = 2;
  enum Mode {
    NORMAL_CHECK_INTERVAL = 0;
    RETRY_CHECK_INTERVAL = 1;
    MAX_ATTEMPTS = 2;
    MODATTR = 3;
  }
  Mode mode = 3;
  uint32 intval = 4;
  double dval = 5;
}

/* used for a host or a service */
message ChangeServiceNumber {
  ServiceIdentifier serv = 1;
  oneof value {
    uint32 intval = 2;
    double dval = 3;
  }
}

/* used for a contact */
message ChangeContactObjectInt {
  string contact_name = 1;
  enum Mode {
    MODATTR = 0;
    MODHATTR = 1;
    MODSATTR = 2;
  }
  Mode mode = 2;
  uint32 intval = 3;
  double dval = 4;
}

/* used for a host or a service */
message ChangeObjectChar {
  string host_name = 1;
  string service_desc = 2;
  enum Mode {
    CHANGE_GLOBAL_EVENT_HANDLER = 0;
    CHANGE_EVENT_HANDLER = 1;
    CHANGE_CHECK_COMMAND = 2;
    CHANGE_CHECK_TIMEPERIOD = 3;
    CHANGE_NOTIFICATION_TIMEPERIOD = 4;
  }
  Mode mode = 3;
  string charval = 4;
}

/* used for a contact */
message ChangeContactObjectChar {
  string contact = 1;
  enum Mode {
    CHANGE_CONTACT_HOST_NOTIFICATION_TIMEPERIOD = 0;
    CHANGE_CONTACT_SVC_NOTIFICATION_TIMEPERIOD = 1;
  }
  Mode mode = 2;
  string charval = 3;
}

message ChangeObjectCustomVar {
  string host_name = 1;
  string service_desc = 2;
  string contact = 3;
  string varname = 4;
  string varvalue = 5;
}

message LogInfo {
  message LoggerInfo {
    string log_name = 1;
    string log_file = 2;
    uint32 log_flush_period = 3;
    map<string, string> level = 4;
  }
  repeated LoggerInfo loggers = 1;
}

message LogFlushPeriod {
  uint32 period = 1;
}

message LogLevel {
  string logger = 1;
  enum LogLevelEnum {
    TRACE = 0;
    DEBUG = 1;
    INFO = 2;
    WARNING = 3;
    ERROR = 4;
    CRITICAL = 5;
    OFF = 6;
  }
  LogLevelEnum level = 2;
}
