-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfluent-start.sh
More file actions
executable file
·140 lines (126 loc) · 4.01 KB
/
fluent-start.sh
File metadata and controls
executable file
·140 lines (126 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
########################################################################
# fluent-start.sh: Fluentd Startup Script
#
# Description:
# This script starts Fluentd with a specified configuration. It sets up
# Fluentd and executes it in the background, along with Fluent-Cat for
# sending test messages. This version improves POSIX compatibility and
# adds error handling for missing commands.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# ./fluent-start.sh [fluentd path] [fluentd conf path] [options]
#
# Version History:
# v0.9 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v0.8 2025-04-13
# Unify log level formatting using [INFO], [WARN], and [ERROR] tags.
# v0.7 2025-03-25
# Add config directory and file existence checks to determine_fluentd_config.
# v0.6 2025-03-22
# Unify usage information by extracting help text from header comments.
# v0.5 2025-03-17
# Encapsulated all logic into functions and introduced main function.
# v0.4 2025-02-26
# Improved POSIX compatibility by replacing `test -n` with `[ -n ]`.
# Added error handling for missing Fluentd and Fluent-Cat commands.
# v0.3 2023-12-20
# Replaced 'which' with 'command -v' for command existence check.
# Updated script header for consistency.
# v0.2 2013-04-08
# Add options.
# v0.1 2013-04-04
# First release.
#
########################################################################
# Display full script header information extracted from the top comment block
usage() {
awk '
BEGIN { in_header = 0 }
/^#{10,}$/ { if (!in_header) { in_header = 1; next } else exit }
in_header && /^# ?/ { print substr($0, 3) }
' "$0"
exit 0
}
# Check if required commands are available and executable
check_commands() {
for cmd in "$@"; do
cmd_path=$(command -v "$cmd" 2>/dev/null)
if [ -z "$cmd_path" ]; then
echo "[ERROR] Command '$cmd' is not installed. Please install $cmd and try again." >&2
exit 127
elif [ ! -x "$cmd_path" ]; then
echo "[ERROR] Command '$cmd' is not executable. Please check the permissions." >&2
exit 126
fi
done
}
# Determine Fluentd and Fluent-Cat paths
determine_fluentd_paths() {
if [ -n "$1" ]; then
FLUENTD=$1/bin/fluentd
FLUENT_CAT=$1/bin/fluent-cat
else
FLUENTD=$(command -v fluentd)
FLUENT_CAT=$(command -v fluent-cat)
fi
}
# Check if Fluentd and Fluent-Cat exist
check_fluentd_commands() {
if [ -z "$FLUENTD" ]; then
echo "[ERROR] Fluentd not found. Please specify the path." >&2
exit 1
fi
if [ -z "$FLUENT_CAT" ]; then
echo "[ERROR] Fluent-Cat not found. Please specify the path." >&2
exit 1
fi
check_commands "$FLUENTD" "$FLUENT_CAT"
}
# Determine Fluentd configuration path
determine_fluentd_config() {
if [ -n "$1" ]; then
FLUENT_CONF=$1
else
FLUENT_CONF="$HOME/.fluent"
fi
# Check if directory exists
if [ ! -d "$FLUENT_CONF" ]; then
echo "[ERROR] Configuration directory not found: $FLUENT_CONF" >&2
exit 1
fi
# Check if fluent.conf exists
if [ ! -f "$FLUENT_CONF/fluent.conf" ]; then
echo "[ERROR] fluent.conf not found in: $FLUENT_CONF" >&2
exit 1
fi
}
# Start Fluentd
start_fluentd() {
$FLUENTD --setup "$FLUENT_CONF"
$FLUENTD -c "$FLUENT_CONF/fluent.conf" "$3" &
}
# Send test message
send_test_message() {
echo '{"json":"message"}' | "$FLUENT_CAT" debug.test
}
# Main entry point of the script
main() {
case "$1" in
-h|--help|-v|--version) usage ;;
esac
determine_fluentd_paths "$1"
check_fluentd_commands
determine_fluentd_config "$2"
start_fluentd "$3"
send_test_message
return 0
}
# Execute main function
main "$@"