Skip to content

Commit

Permalink
Localhost linux (#27)
Browse files Browse the repository at this point in the history
* localhost-linux

* configs

* fix

* fix

* fix jq

* fix tar

* update info

* troubleshooting

* remove processes

* add dashboard

* otel or service exist

* fix
  • Loading branch information
ShiranAvidov authored Sep 5, 2022
1 parent 1fd9792 commit d030cf8
Show file tree
Hide file tree
Showing 17 changed files with 892 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Localhost/Linux/System/prerequisites/linux/functions.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

#################################################################################################################################
################################################ Prerequisites Linux Functions ##################################################
#################################################################################################################################

# Installs yq
# Output:
# yq_bin - The yq binary file path
# Error:
# Exit Code 1
function install_yq () {
write_log "INFO" "Installing yq ..."
curl -fsSL https://github.com/mikefarah/yq/releases/download/v4.27.2/yq_linux_amd64.tar.gz > $logzio_temp_dir/yq.tar.gz 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"prerequisites.bash (1): failed to get yq binary file from Github.\n $err\""
return 1
fi

yq_bin="$logzio_temp_dir/yq_linux_amd64"
tar -zxf $logzio_temp_dir/yq.tar.gz --directory $logzio_temp_dir
write_run "yq_bin=\"$yq_bin\""
}

# Checks if localhost can connect to Logz.io logs (port 8071)
# Error:
# Exit Code 2
function can_localhost_connect_to_logzio_logs () {
write_log "INFO" "Checking if localhost can connect to Logz.io logs (port 8071) ..."

wget -q -t 3 --timeout=5 listener.logz.io:8071
if [[ $? -eq 8 ]]; then
return
fi

write_run "print_error \"prerequisites.bash (2): localhost cannot connect to Logz.io logs. please check your network for port 8071\""
return 2
}

# Checks if localhost can connect to Logz.io metrics (port 8053)
# Error:
# Exit Code 2
function can_localhost_connect_to_logzio_metrics () {
write_log "INFO" "Checking if localhost can connect to Logz.io metrics (port 8053) ..."

wget -q -t 3 --timeout=5 listener.logz.io:8053
if [[ $? -eq 8 ]]; then
return
fi

write_run "print_error \"prerequisites.bash (2): localhost cannot connect to Logz.io logs. please check your network for port 8053\""
return 2
}
18 changes: 18 additions & 0 deletions Localhost/Linux/System/prerequisites/linux/prerequisites.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

#################################################################################################################################
################################################## Prerequisites Linux Script ###################################################
#################################################################################################################################

# Load functions
write_log "INFO" "Loading prerequisites functions ..."
source ./logzio-temp/prerequisites_functions.bash

# Install yq
execute_task "install_yq" "installing yq"

# Check if localhost can connect to Logz.io logs (port 8071)
execute_task "can_localhost_connect_to_logzio_logs" "checking if localhost can connect to Logz.io logs"

# Check if localhost can connect to Logz.io metrics (port 8053)
execute_task "can_localhost_connect_to_logzio_metrics" "checking if localhost can connect to Logz.io metrics"
218 changes: 218 additions & 0 deletions Localhost/Linux/System/telemetry/installer/linux/functions.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#!/bin/bash

#################################################################################################################################
################################################## Installer Linux Functions ####################################################
#################################################################################################################################

# Checks if Logz.io OTEL collector service exist
# Output:
# is_service_exist - Tells if Logz.io OTEL collector service exist
function is_logzio_otel_collector_service_exist () {
write_log "INFO" "Checking if Logz.io OTEL collector service exist ..."

local service=$(systemctl | grep logzioOTELCollector)
if [[ -z $service ]]; then
write_log "is_service_exist = false"
write_run "is_service_exist=false"
return
fi

write_log "is_service_exist = true"
write_run "is_service_exist=true"
}

# Gets the selected products (logs/metrics)
# Output:
# is_logs_option_selected - Tells if logs option was selected (true/false)
# logs_params - The logs params if logs option was selected
# is_metrics_option_selected - Tells if metrics option was selected (true/false)
# metrics_params - The metrics params if metrics option was selected
# Error:
# Exit Code 1
function get_selected_products () {
write_log "INFO" "Getting the selected products ..."

local telemetries=$(jq -c '.configuration.subtypes[0].datasources[0].telemetries[]' $app_json)
if [[ "$telemetries" = null ]]; then
write_run "print_error \"installer.bash (1): .configuration.subtypes[0].datasources[0].telemetries[] was not found in application JSON\""
return 1
fi
if [[ -z "$telemetries" ]]; then
write_run "print_error \"installer.bash (1): .configuration.subtypes[0].datasources[0].telemetries[] is empty in application JSON\""
return 1
fi

local is_logs_option_selected=false
local is_metrics_option_selected=false
local index=0

while read -r telemetry; do
local type=$(echo "$telemetry" | jq -r '.type')
if [[ "$type" = null ]]; then
write_run "print_error \"installer.bash (1): '.configuration.subtypes[0].datasources[0].telemetries[$index].type' was not found in application JSON\""
return 1
fi
if [[ -z "$type" ]]; then
write_run "print_error \"installer.bash (1): '.configuration.subtypes[0].datasources[0].telemetries[$index].type' is empty in application JSON\""
return 1
fi

local params=$(echo -e "$telemetry" | jq -r '.params[]')
if [[ "$params" = null ]]; then
write_run "print_error \"installer.bash (1): '.configuration.subtypes[0].datasources[0].telemetries[$index].params[]' was not found in application JSON\""
return 1
fi

if [[ "$type" = "LOG_ANALYTICS" ]]; then
write_log "INFO" "is_logs_option_selected = true"
write_log "INFO" "logs_params = $params"

is_logs_option_selected=true
write_run "logs_params='$params'"
elif [[ "$type" = "METRICS" ]]; then
write_log "INFO" "is_metrics_option_selected = true"
write_log "INFO" "metrics_params = $params"

is_metrics_option_selected=true
write_run "metrics_params='$params'"
fi

let "index++"
done < <(echo -e "$telemetries")

write_run "is_logs_option_selected=$is_logs_option_selected"
write_run "is_metrics_option_selected=$is_metrics_option_selected"
}

# Creates Logz.io opt directory
# Output:
# logzio_opt_dir - The Logz.io opt directory path
function create_logzio_opt_dir () {
write_log "INFO" "Creating Logz.io opt directory ..."

logzio_opt_dir="/opt/logzio-otel-collector"
mkdir -p $logzio_opt_dir
write_run "logzio_opt_dir=\"$logzio_opt_dir\""
}

# Gets OTEL collector binary
# Output:
# otel_bin - The OTEL collector binary file path
# Error:
# Exit Code 2
function get_otel_collector_binary () {
write_log "INFO" "Getting OTEL collector binary ..."
curl -fsSL https://github.com/logzio/otel-collector-distro/releases/download/v0.56.1/otelcol-logzio-linux_amd64.tar.gz > $logzio_temp_dir/otelcol-logzio.tar.gz 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"instalelr.bash (2): failed to get OTEL collector binary file from Logz.io repo.\n $err\""
return 2
fi

otel_bin="$logzio_opt_dir/otelcol-logzio-linux_amd64"
tar -zxf $logzio_temp_dir/otelcol-logzio.tar.gz --directory $logzio_opt_dir
write_run "otel_bin=\"$otel_bin\""
}

# Gets OTEL config from logzio-agent-manifest repo
# Output:
# otel_config - The OTEL config file path
# Error:
# Exit Code 3
function get_otel_config () {
write_log "INFO" "Getting OTEL config file from logzio-agent-manifest repo ..."

otel_config="$logzio_opt_dir/otel_config.yaml"
curl -fsSL $repo_path/telemetry/installer/otel_config.yaml > $otel_config 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"installer.bash (3): failed to get OTEL config file from logzio-agent-manifest repo.\n $err\""
return 3
fi

write_run "otel_config=\"$otel_config\""
}

# Gets service file from logzio-agent-manifest repo
# Output:
# service_name - The Logz.io OTEL collector service name
# Error:
# Exit Code 4
function get_logzio_otel_collector_service_file () {
write_log "INFO" "Getting Logz.io OTEL collector service file ..."

service_name="logzioOTELCollector"
curl -fsSL $repo_path/telemetry/installer/logzioOTELCollector.service > /etc/systemd/system/$service_name.service 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"installer.bash (4): failed to get Logz.io OTEL collector service file from logzio-agent-manifest repo.\n $err\""
return 4
fi

write_run "service_name=\"$service_name\""
}

# Gets logs scripts from logzio-agent-manifest repo
# Error:
# Exit Code 5
function get_logs_scripts () {
write_log "INFO" "Getting logs script file from logzio-agent-manifest repo ..."
curl -fsSL $repo_path/telemetry/logs/linux/logs.bash > $logzio_temp_dir/logs.bash 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"installer.bash (5): failed to get logs script file from logzio-agent-manifest repo.\n $err\""
return 5
fi

write_log "INFO" "Getting logs functions script file from logzio-agent-manifest repo ..."
curl -fsSL $repo_path/telemetry/logs/linux/functions.bash > $logzio_temp_dir/logs_functions.bash 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"installer.bash (5): failed to get logs functions script file from logzio-agent-manifest repo.\n $err\""
return 5
fi
}

# Gets metrics scripts from logzio-agent-manifest repo
# Error:
# Exit Code 6
function get_metrics_scripts () {
write_log "INFO" "Getting metrics script file from logzio-agent-manifest repo ..."
curl -fsSL $repo_path/telemetry/metrics/linux/metrics.bash > $logzio_temp_dir/metrics.bash 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"installer.bash (6): failed to get metrics script file from logzio-agent-manifest repo.\n $err\""
return 6
fi

write_log "INFO" "Getting metrics functions script file from logzio-agent-manifest repo ..."
curl -fsSL $repo_path/telemetry/metrics/linux/functions.bash > $logzio_temp_dir/metrics_functions.bash 2>$task_error_file
if [[ $? -ne 0 ]]; then
local err=$(cat $task_error_file)
write_run "print_error \"installer.bash (6): failed to get metrics functions script file from logzio-agent-manifest repo.\n $err\""
return 6
fi
}

# Runs Logz.io OTEL collector service
# Error:
# Exit Code 7
function run_logzio_otel_collector_service () {
write_log "INFO" "Running Logz.io OTEL collector service ..."
write_log "INFO" "OTEL config =\n$(cat $otel_config)"

systemctl start $service_name >/dev/null 2>$task_error_file
local err=$(cat $task_error_file)
if [[ ! -z "$err" ]]; then
write_run "print_error \"installer.bash (7): failed to start Logz.io OTEL collector service.\n $err\""
return 7
fi

is_running=$(systemctl status $service_name | grep "$service_name.service failed")
if [[ -z "$is_running" ]]; then
return
fi

write_run "print_error \"installer.bash (7): failed to run Logz.io OTEL collector service.\n Run `sudo systemctl status -l $service_name` to see the error.\""
return 7
}
84 changes: 84 additions & 0 deletions Localhost/Linux/System/telemetry/installer/linux/installer.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

#################################################################################################################################
#################################################### Installer Linux Script #####################################################
#################################################################################################################################

# Load functions
write_log "INFO" "Loading installer functions ..."
source $logzio_temp_dir/installer_functions.bash

# Check if Logz.io OTEL collector service exist
execute_task "is_logzio_otel_collector_service_exist" "checking if Logz.io OTEL collector service exist"
if $is_service_exist; then
while true; do
echo -ne "\033[0;33mcom.logzio.OTELCollector service is already exist. If you continue the service will be removed. Are you sure? (y/n)\033[0;37m "
read answer
if [[ "$answer" = "y" ]]; then
systemctl stop logzioOTELCollector >/dev/null 2>&1
break
elif [[ "$answer" = "n" ]]; then
tput cnorm -- normal
delete_temp_dir
exit
fi
done
echo
fi

# Get the selected products
execute_task "get_selected_products" "getting the selected products"

# Create Logz.io opt directory
execute_task "create_logzio_opt_dir" "creating Logz.io opt directory"

# Get OTEL collector binary
execute_task "get_otel_collector_binary" "getting OTEL collector binary"

# Get OTEL config
execute_task "get_otel_config" "getting OTEL config"

# Get Logz.io OTEL collector service file
execute_task "get_logzio_otel_collector_service_file" "getting Logz.io OTEL collector service file"

# Get logs scripts
if $is_logs_option_selected; then
execute_task "get_logs_scripts" "getting logs scripts"
fi

# Get metrics scripts
if $is_metrics_option_selected; then
execute_task "get_metrics_scripts" "getting metrics scripts"
fi

# Run logs script
if $is_logs_option_selected; then
write_log "INFO" "Running logs script ..."
echo -e "\nlogs:"
source $logzio_temp_dir/logs.bash
fi

# Run metrics script
if $is_metrics_option_selected; then
write_log "INFO" "Running metrics script ..."
echo -e "\nmetrics:"
source $logzio_temp_dir/metrics.bash
fi

# Run Logz.io OTEL collector service
echo -e "\ninstaller:"
execute_task "run_logzio_otel_collector_service" "running Logz.io OTEL collector service"

# Print success message
echo
print_info "##### Logz.io agent was finished successfully #####"

# Print information
echo -e "\nInformation:\n"
echo -e "\033[0;35mCollector Binary\033[0;37m: $otel_bin"
echo -e "\033[0;35mCollector Config\033[0;37m: $otel_config"
echo -e "\033[0;35mStart Service Command\033[0;37m: sudo systemctl start $service_name"
echo -e "\033[0;35mStop Service Command\033[0;37m: sudo systemctl stop $service_name"
echo -e "\033[0;35mShow Service Command\033[0;37m: sudo systemctl | grep $service_name"
echo -e "\033[0;35mShow Logs Command\033[0;37m: sudo systemctl status -l $service_name"
echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
Description=OTEL collector for collecting logs/metrics and exporting them to Logz.io.

[Service]
ExecStart=/opt/logzio-otel-collector/otelcol-logzio-linux_amd64 --config /opt/logzio-otel-collector/otel_config.yaml

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit d030cf8

Please sign in to comment.