-
Notifications
You must be signed in to change notification settings - Fork 49
/
linode-longview.config
executable file
·211 lines (181 loc) · 7.49 KB
/
linode-longview.config
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/sh
set -e
longview_config_dir='/etc/linode/longview.d/'
echoerr() { echo "$@" 1>&2; }
_fake_curl() {
_input_url=$1
PERL_LWP_SSL_VERIFY_HOSTNAME=0 perl -MLWP::UserAgent -e '$ua=LWP::UserAgent->new(); print $ua->get("$ARGV[0]")->decoded_content;' "$1"
return 0
}
_check_apache_url() {
_apache_url=$1
_fake_curl "$_apache_url" | grep -q 'Scoreboard:'
return $?
}
_check_nginx_url() {
_nginx_url=$1
_fake_curl "$_nginx_url" | grep -q 'server accepts handled requests'
return $?
}
_mysql_check_install() {
_mysql_username=$1
_mysql_password=$2
perl -MDBI -e "eval { local \$SIG{ALRM} = sub { die; }; alarm(5); \$dbh = DBI->connect('DBI:mysql:host=localhost', '$_mysql_username', '$_mysql_password'); exit \$DBI::err; alarm(0); }; exit 1 if \$@"
return $?
}
apache_status_path() {
echoerr "Checking Apache configuration..."
apache_config_filename=${longview_config_dir}Apache.conf
apache_default_url='http://127.0.0.1/server-status?auto'
apache_default_https='https://127.0.0.1/server-status?auto'
apache_url=$(grep '^location' $apache_config_filename | tail -n1 | awk '{print $2}')
# use the url if it exists
if [ ! -z $apache_url ]; then
if _check_apache_url $apache_url ; then
echoerr "Found Apache status page at $apache_url specified in $apache_config_filename"
db_set Longview/apache-location ""
return 0
elif [ "$apache_url" = "$apache_default_url" ]; then
echoerr "Attempt to access $apache_url specified in $apache_config_filename failed..."
else
echoerr "Attempt to access $apache_url specified in $apache_config_filename failed...trying default"
if _check_apache_url $apache_default_url; then
echoerr "Found Apache status page at $apache_default_url (default URL)"
db_set Longview/apache-location "$apache_default_url"
return 0;
elif _check_apache_url $apache_default_https; then
echoerr "Found Apache status page at $apache_default_https"
db_set Longview/apache-location "$apache_default_https"
return 0;
fi
fi
else
if _check_apache_url $apache_default_url; then
echoerr "Found Apache status page at $apache_default_url (default URL)"
db_set Longview/apache-location "$apache_default_url"
return 0;
elif _check_apache_url $apache_default_https; then
echoerr "Found Apache status page at $apache_default_https"
db_set Longview/apache-location "$apache_default_https"
return 0;
fi
fi
# really ghetto url extraction
guess_url=$(grep '^ *<Location' /etc/apache2/mods-enabled/status.conf 2>/dev/null | head -n1 | sed 's/>$//' | awk '{print $2}')
if [ ! -z $guess_url ]; then
if _check_apache_url http://127.0.0.1$guess_url?auto; then
echoerr "Found Apache status page at http://127.0.0.1$guess_url?auto"
db_set Longview/apache-location "http://127.0.0.1$guess_url?auto"
return 0
fi
fi
db_set Longview/apache-location "$apache_default_url"
db_fset Longview/apache-try-configure seen "false"
db_input high Longview/apache-try-configure
}
nginx_status_path(){
echoerr "Checking Nginx configuration..."
nginx_config_filename=${longview_config_dir}Nginx.conf
nginx_default_url='http://127.0.0.1/nginx_status'
nginx_default_https='https://127.0.0.1/nginx_status'
nginx_url=$(grep '^location' $nginx_config_filename 2>/dev/null | tail -n1 | awk '{print $2}')
# use the url if it exists
if [ ! -z $nginx_url ]; then
if _check_nginx_url $nginx_url; then
echoerr "Found nginx status page at $nginx_url specified in $nginx_config_filename"
db_set Longview/nginx-location ""
return 0
elif [ "$nginx_url" = "$nginx_default_url" ]; then
echoerr "Attempt to access $nginx_url specified in $nginx_config_filename failed..."
else
echoerr "Attempt to access $nginx_url specified in $nginx_config_filename failed...trying default"
if _check_nginx_url $nginx_default_url; then
echoerr "Found nginx status page at $nginx_default_url (default URL)"
db_set Longview/nginx-location ""
return 0;
elif _check_nginx_url $nginx_default_https; then
echoerr "Found nginx status page at $nginx_default_https"
db_set Longview/nginx-location "$nginx_default_https"
return 0;
fi
fi
fi
if _check_nginx_url $nginx_default_url; then
echoerr "Found nginx status page at $nginx_default_url (default URL)"
db_set Longview/nginx-location ""
return 0;
elif _check_nginx_url $nginx_default_https; then
echoerr "Found nginx status page at $nginx_default_https"
db_set Longview/nginx-location "$nginx_default_https"
return 0;
fi
# don't be smart unless we know where to work
if [ ! -d /etc/nginx/sites-enabled ]; then
echoerr "Unable to automatically configure nginx for Longview. Please see https://library.linode.com/longview/longview-for-nginx#sph_manual-configuration-all-distributions"
db_set Longview/nginx-location ""
return 0
fi
# this is kind of weird
# check to make sure that the haxor mode is gonna work
if grep -ri '^[[:space:]]*server_name.*127\.0\.0\.2' /etc/nginx/sites-enabled; then
echoerr "Unable to automatically configure nginx for Longview. Please see https://library.linode.com/longview/longview-for-nginx#sph_manual-configuration-all-distributions"
db_set Longview/nginx-location ""
return 0
fi
if [ -e /etc/nginx/sites-available/longview ]; then
if _check_nginx_url http://127.0.0.2/nginx_status; then
echoerr "Found location configured by Longview previously."
db_set Longview/nginx-location "http://127.0.0.2/nginx_status"
return 0
fi
echoerr "Unable to automatically configure nginx for Longview. Please see https://library.linode.com/longview/longview-for-nginx#sph_manual-configuration-all-distributions"
db_set Longview/nginx-location ""
return 0
fi
db_input high Longview/nginx-try-configure || true
}
mysql_cred_check() {
echoerr "Checking MySQL configuration..."
mysql_config_filename=${longview_config_dir}MySQL.conf
mysql_username=$(grep '^username' $mysql_config_filename 2>/dev/null | tail -n1 | awk '{print $2}')
mysql_password=$(grep '^password' $mysql_config_filename 2>/dev/null | tail -n1 | awk '{print $2}')
if [ ! -z $mysql_username ] && [ ! -z $mysql_password ]; then
if _mysql_check_install $mysql_username $mysql_password; then
echoerr "Successfully connected to MySQL with the credentials located in $mysql_config_filename"
db_set Longview/mysql-password ""
return 0
else
echoerr "Unable to connect using the credentials specified in $mysql_config_filename...trying Debian maintenance user"
fi
fi
if [ -e /etc/mysql/debian.cnf ]; then
mysql_password=$(egrep '^\s*password\s*=\s*' /etc/mysql/debian.cnf | head -n1 | sed 's/^\s*password\s*=\s*//')
if _mysql_check_install debian-sys-maint $mysql_password; then
echoerr "Successfully connected to MySQL"
db_set Longview/mysql-password ""
if [ ! -z $mysql_username ] && [ ! -z $mysql_password ]; then
db_subst Longview/mysql-overwrite-creds found_user "$mysql_username"
db_input high Longview/mysql-overwrite-creds || true
fi
return 0
fi
fi
mysql_username='linode-longview'
mysql_password=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32)
create_statement="CREATE USER 'linode-longview'@'localhost' IDENTIFIED BY '$mysql_password';"
db_set Longview/mysql-password "$mysql_password"
db_subst Longview/mysql-create-creds create_statement "$create_statement"
db_input high Longview/mysql-create-creds || true
}
. /usr/share/debconf/confmodule
db_input medium Longview/APIKey || true
if pgrep -f 'httpd|apache2' >/dev/null; then
apache_status_path
fi
if pgrep -f 'nginx' >/dev/null; then
nginx_status_path
fi
if pgrep -f 'mysqld' >/dev/null; then
mysql_cred_check
fi
db_go || true