-
Notifications
You must be signed in to change notification settings - Fork 5
/
fixes15.sh
executable file
·928 lines (782 loc) · 34.8 KB
/
fixes15.sh
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#!/bin/bash
# No need to run this directly.
# Instead, run: sudo nems-update
online=$(/usr/local/share/nems/nems-scripts/info.sh online)
if [[ $online == 0 ]]; then
echo "Internet is offline. NEMS needs Internet connectivity."
echo ""
exit
fi
# Just in case nems-quickfix is running
fixes=$(/usr/local/bin/nems-info fixes)
if [[ $fixes == 1 ]]; then
echo 'NEMS Linux is currently updating itself. Please wait...'
while [[ $fixes == 1 ]]
do
sleep 1
fixes=$(/usr/local/bin/nems-info fixes)
done
fi
echo $$ > /var/run/nems-fixes.pid
# By default, do not reboot after update
reboot=0
# Setup a patches.log file if one doesn't exist
# This ensures once a patch is run, it doesn't run again
# It can also be used to cross-reference the changelogs to
# see what patches have been added to your NEMS server.
if [[ ! -e /var/log/nems/patches.log ]]; then
touch /var/log/nems/patches.log
fi
# Just in case apt is already doing stuff in the background, hang tight until it completes
echo "Please wait for apt tasks to complete..."
while fuser /var/{lib/{dpkg,apt/lists},cache/apt/archives}/lock >/dev/null 2>&1; do sleep 1; done
echo "Done."
# Make sure /bin/systemctl resolves
if [[ ! -e /bin/systemctl ]]; then
if [[ -e /usr/bin/systemctl ]]; then
ln -s /usr/bin/systemctl /bin/systemctl
fi
fi
# using hard file location rather than symlink as symlink may not exist yet on older versions
platform=$(/usr/local/share/nems/nems-scripts/info.sh platform)
ver=$(/usr/local/share/nems/nems-scripts/info.sh nemsver)
# Ensure apt has the pubkeys needed
if [[ $(apt-key list 2> /dev/null | grep DEB.SURY.ORG) == "" ]]; then
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B188E2B695BD4743
fi
# Update apt here so we don't have to do it below
apt-get clean
if (( $platform >= 0 )) && (( $platform <= 9 )); then
apt-get update --allow-releaseinfo-change
else
apt-get update
fi
# Fix Default Collector name if incorrect
collector=$(/usr/bin/mysql -u nconf -h 127.0.0.1 -pnagiosadmin -D nconf -e "SELECT attr_value FROM ConfigValues WHERE fk_id_attr = 1;")
if [[ ! $collector = *"Default Nagios"* ]]; then
/usr/bin/mysql -u nconf -h 127.0.0.1 -pnagiosadmin -D nconf -e "UPDATE ConfigValues SET attr_value='Default Nagios' WHERE fk_id_attr = 1;"
fi
# Make a symlink to the PHP interpreter if it doesn't exist in /usr/local/bin
if [[ ! -e /usr/local/bin/php ]]; then
if [[ -e /usr/bin/php ]]; then
ln -s /usr/bin/php /usr/local/bin/php
fi
fi
if (( $(awk 'BEGIN {print ("'$ver'" >= "'1.6'")}') )); then
# Symlink python2 binary to python3 to prevent errors for scripts that call /usr/bin/python
# This method allows us to keep compatibility with old versions of NEMS Linux a little more easily
if [[ ! -e /usr/bin/python ]] && [[ -e /usr/bin/python3 ]]; then
ln -s /usr/bin/python3 /usr/bin/python
fi
fi
if [[ "$ver" == "1.4" ]]; then
# Fix Nagios lockfile location (was causing systemd to be unable to restart Nagios)
# Fix the original attempt
if grep -q "lock_file=/var/lock/subsys/nagios" /usr/local/nagios/etc/nagios.cfg; then
/bin/systemctl stop monit
/bin/systemctl stop nagios
echo Changing location of Nagios lock file in Nagios config...
/bin/sed -i -- 's,lock_file=/var/lock/subsys/nagios,lock_file=/run/nagios.lock,g' /usr/local/nagios/etc/nagios.cfg
/usr/bin/killall -9 nagios
sleep 1
echo Done.
fi
if grep -q "/var/lock/subsys/nagios" /etc/monit/conf.d/nems.conf; then
/bin/systemctl stop monit
/bin/systemctl stop nagios
echo Changing location of Nagios lock file in Monit...
/bin/sed -i -- 's,/var/lock/subsys/nagios,/run/nagios.lock,g' /etc/monit/conf.d/nems.conf
/usr/bin/killall -9 nagios
sleep 1
echo Done.
fi
# Actual fix
if grep -q "lock_file=/var/run/nagios/nagios.pid" /usr/local/nagios/etc/nagios.cfg; then
/bin/systemctl stop monit
/bin/systemctl stop nagios
echo Changing location of Nagios lock file in Nagios config...
/bin/sed -i -- 's,lock_file=/var/run/nagios/nagios.pid,lock_file=/run/nagios.lock,g' /usr/local/nagios/etc/nagios.cfg
/usr/bin/killall -9 nagios
sleep 1
echo Done.
fi
if grep -q "/run/nagios/nagios.pid" /etc/monit/conf.d/nems.conf; then
/bin/systemctl stop monit
/bin/systemctl stop nagios
echo Changing location of Nagios lock file in Monit...
/bin/sed -i -- 's,/run/nagios/nagios.pid,/run/nagios.lock,g' /etc/monit/conf.d/nems.conf
/usr/bin/killall -9 nagios
sleep 1
echo Done.
fi
/bin/systemctl start nagios
/bin/systemctl start monit
# /Fix Nagios lockfile location (was causing systemd to be unable to restart Nagios)
# Add a symlink to the check_nrpe executable from within the correct Nagios plugin folder
if [[ ! -e /usr/local/nagios/libexec/check_nrpe ]]; then
if [[ -f /usr/lib/nagios/plugins/check_nrpe ]]; then
ln -s /usr/lib/nagios/plugins/check_nrpe /usr/local/nagios/libexec/check_nrpe
fi
fi
# Activate automatic security updates
if [[ ! -e /etc/apt/apt.conf.d/20auto-upgrades ]]; then
if [[ -f /root/nems/nems-admin/build/025-auto-upgrades ]]; then
/root/nems/nems-admin/build/025-auto-upgrades
fi
fi
# Fix RPi-Monitor CPU Frequency Reporting
if (( $platform >= 0 )) && (( $platform <= 9 )); then
if [[ ! -e /usr/bin/vcgencmd ]]; then
apt-get -y install libraspberrypi-bin
/bin/systemctl restart rpimonitor
fi
fi
# NEMS 1.4 upgraded to 1.4.1 now that Raspberry Pi Zero W is fully supported
# Because NEMS 1.4.1 meant a new image (for Pi Zero W) we'll roll up all 1.4 systems
/bin/sed -i -e "s/1.4/1.4.1/g" /usr/local/share/nems/nems.conf
fi
# Fix strange issue where some systems got bumped to 1.4.1.1
if grep -q "version=1.4.1.1" /usr/local/share/nems/nems.conf; then
/bin/sed -i -e "s/1.4.1.1/1.4.1/g" /usr/local/share/nems/nems.conf
ver="1.4.1"
fi
if [[ "$ver" == "1.4.1" ]]; then
# Fix permissions for Nagios log archiving
chmod ug+x /usr/local/nagios/var/archives
# Remove NEMS00000 patch from adagios.conf. It caused problems if user renamed admin user in NConf.
if grep -q "# NEMS00000 A hacky way of disabling the admin portions of Adagios" /etc/adagios/adagios.conf; then
/bin/sed -i~ '/# NEMS00000/d' /etc/adagios/adagios.conf
/bin/sed -i~ '/enable_authorization=True/d' /etc/adagios/adagios.conf
/bin/sed -i~ '/administrators="nobodyisadmin"/d' /etc/adagios/adagios.conf
fi
# Fix ownership of Nagios logs folder
chown -R nagios:nagios /var/log/nagios
# Fix ownership of new Adagios web folder (which is now a symlink)
chown -R www-data:www-data /var/www/adagios/
# Remove Izzy's repository (at least temporarily).
# cert is broken and it causes all kinds of grief.
# Perhaps need to evaluate building monitorix ourselves.
if [[ -f /etc/apt/sources.list.d/monitorix.list ]]; then
rm /etc/apt/sources.list.d/monitorix.list
fi
# Allow the NEMS user to also administer nagios, access livestatus, etc.
username=$(/usr/local/bin/nems-info username)
usermod -a -G www-data,nagios $username
# Install phoronix test suite to supplement our benchmarks
if [[ ! -f /usr/bin/phoronix-test-suite ]]; then
if [[ -f /root/nems/nems-admin/build/221-phoronix ]]; then
/root/nems/nems-admin/build/221-phoronix
fi
fi
# Install less command
if [[ ! -e /usr/bin/less ]]; then
apt-get -y install less
fi
# Install nems-tools
if [[ ! -d /root/nems/nems-tools ]]; then
cd /root/nems
git clone https://github.com/Cat5TV/nems-tools
fi
# Clean up log errors from early build of nems-tools
restartwarninglight=0 # Default
if grep -q "PHP Warning" /var/log/nems/nems-tools/warninglight; then
/bin/sed -i~ '/PHP Warning/d' /var/log/nems/nems-tools/warninglight
restartwarninglight=1
fi
if grep -q "PHP Notice" /var/log/nems/nems-tools/warninglight; then
/bin/sed -i~ '/PHP Notice/d' /var/log/nems/nems-tools/warninglight
restartwarninglight=1
fi
if grep -q "sh: 1: /usr/local/bin/gpio: not found" /var/log/nems/nems-tools/warninglight; then
/bin/sed -i~ '/sh: 1: \/usr\/local\/bin\/gpio: not found/d' /var/log/nems/nems-tools/warninglight
restartwarninglight=1
fi
if grep -q "Parameter must be an array or an object that implements Countable" /var/log/nems/nems-tools/warninglight; then
/bin/sed -i~ '/Parameter must be an array or an object that implements Countable/d' /var/log/nems/nems-tools/warninglight
restartwarninglight=1
fi
# Restart warninglight if needed
if (( $restartwarninglight == 1 )); then
kill `cat /var/run/warninglight.pid` && sleep 1
/root/nems/nems-tools/warninglight >> /var/log/nems/nems-tools/warninglight 2>&1 &
fi
# Install glances command
if [[ ! -e /usr/bin/glances ]]; then
apt-get -y install glances
fi
# Check if nagios-plugins was compiled with openssl. If not, recompile.
checkssl=`/usr/local/nagios/libexec/check_http -S`
if [[ $checkssl =~ 'SSL is not available' ]];
then
# Nagios Plugins was not compiled with SSL, so re-compile (was fixed November 14, 2018)
/root/nems/nems-admin/build/051-nagios-plugins
fi
# Create the patch log dir
if [ ! -d /var/log/nems/patches ]; then
mkdir -p /var/log/nems/patches
fi
# Fix WiFi
if [ ! -f /var/log/nems/patches/20181201-wifi ]; then
online=$(/usr/local/share/nems/nems-scripts/info.sh online)
if [[ $online == 1 ]]; then
# Pi Specific
if (( $platform >= 0 )) && (( $platform <= 9 )); then
apt-get -y install raspberrypi-net-mods
fi
# This is the firmware for RPi WiFi but include for other boards in case needed
# May not be available and may say not found, but this only runs once, so no worries
apt-get -y install firmware-brcm80211
apt-get -y install dhcpcd5
apt-get -y install wireless-tools
apt-get -y install wpasupplicant
# Simple prevention of doing this every time fixes.sh runs
installcheck=`/usr/bin/apt --installed -qq list dhcpcd5`
if [[ $installcheck != '' ]]; then
echo "Patched" > /var/log/nems/patches/20181201-wifi
reboot=1 # Force reboot after this update to activate the firmware
else
echo "Patch appears to have failed"
fi
fi
fi
fi
# end 1.4.1
if (( $(awk 'BEGIN {print ("'$ver'" >= "'1.4.1'")}') )); then
# Benchmarks have not been run yet. Force the first-run (will also run every Sunday on Cron)
if [[ ! -d /var/log/nems/benchmarks ]] || [[ ! -f /var/log/nems/benchmarks/7z-multithread ]]; then
/usr/local/share/nems/nems-scripts/benchmark.sh
fi
if ! grep -q "NEMS00000" /etc/monit/conf.d/nems.conf; then
echo '# NEMS00000 Monitorix
check process monitorix with pidfile /run/monitorix.pid
start program = "/etc/init.d/monitorix start"
stop program = "/etc/init.d/monitorix stop"
' >> /etc/monit/conf.d/nems.conf
/bin/systemctl restart monit
fi
# Install 9590
# A simple listener on Port 9590 for documentation examples
if [[ ! -f /etc/init.d/9590 ]]; then
cp /root/nems/nems-migrator/data/1.4/init.d/9590 /etc/init.d/
/usr/sbin/update-rc.d 9590 defaults
/usr/sbin/update-rc.d 9590 enable
/etc/init.d/9590 start
fi
# And add it to monit
if ! grep -q "NEMS00001" /etc/monit/conf.d/nems.conf; then
echo '# NEMS00001 9590
check process 9590 with pidfile /run/9590.pid
start program = "/etc/init.d/9590 start"
stop program = "/etc/init.d/9590 stop"
' >> /etc/monit/conf.d/nems.conf
/bin/systemctl restart monit
fi
# Stop TTY1 from blanking since keyboard is likely not connected
if [[ -e /etc/rc.local ]]; then
if ! grep -q "NEMS00000" /etc/rc.local; then
/root/nems/nems-admin/build/011-tty
fi
fi
fi
if (( $(awk 'BEGIN {print ("'$ver'" == "'1.5.2'")}') )); then
if ! grep -q "PATCH-000014" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000014
fi
fi
if ! grep -q "PATCH-000015" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000015
fi
if (( $(awk 'BEGIN {print ("'$ver'" >= "'1.5'")}') )); then
# Setup Vendor capabilities
if [[ ! -e /boot/vendor ]]; then
mkdir /boot/vendor
fi
# Create missing usdisks directory as per Cockpit Issue # 12412
# https://github.com/cockpit-project/cockpit/issues/12412
if [[ ! -e /usr/lib/arm-linux-gnueabihf/udisks2/modules ]]; then
mkdir -p /usr/lib/arm-linux-gnueabihf/udisks2/modules
fi
# Add custom_check_mem
if [[ ! -e /usr/lib/nagios/plugins/custom_check_mem ]]; then
apt-get install -y gawk
cp /root/nems/nems-migrator/data/1.5/nagios/plugins/custom_check_mem /usr/lib/nagios/plugins
printf -- "\e[37mImporting:\e[97m custom_check_mem checkcommand to NEMS NConf\033[0m\n" && /var/www/nconf/bin/add_items_from_nagios.pl -c checkcommand -f /root/nems/nems-migrator/data/1.5/nagios/nconf_patches/custom_check_mem-checkcommand.cfg 2>&1 | grep -E "ERROR"
printf -- "\e[37mImporting:\e[97m custom_check_mem advanced service to NEMS NConf\033[0m\n" && /var/www/nconf/bin/add_items_from_nagios.pl -c advanced-service -f /root/nems/nems-migrator/data/1.5/nagios/nconf_patches/custom_check_mem-advanced-service.cfg 2>&1 | grep -E "ERROR"
fi
# A little gift for my faithful users: Access to NagiosTV ahead of the 1.6 release!
if ! grep -q "PATCH-000008" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000008
fi
if ! grep -q "PATCH-000009" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000009
fi
if [[ ! -e /etc/apache2/conf-enabled/nagiostv.conf ]]; then
/root/nems/nems-admin/build/146-nagiostv
# Enable nagiostv
a2enconf nagiostv
# Reload apache2
systemctl reload apache2
fi
# Upgrade NagVis (fixes user creation)
if ! grep -q "PATCH-000010" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000010
fi
# Install WMIC and the insert script for NagiosGraph (Were missing in 1.5.2)
if ! grep -q "PATCH-000011" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000011
fi
# Setup logrotate
if ! grep -q "PATCH-000013" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000013
fi
# Install TEMPer Hardware Support
if [ $(dpkg-query -W -f='${Status}' python3-serial 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
/root/nems/nems-admin/build/500-temper
fi
# Install check_temper script
if [[ ! -e /usr/lib/nagios/plugins/check_temper ]]; then
cp /root/nems/nems-migrator/data/1.5/nagios/plugins/check_temper /usr/lib/nagios/plugins/check_temper
printf -- "\e[37mImporting:\e[97m check_temper to NEMS NConf\033[0m\n" && /var/www/nconf/bin/add_items_from_nagios.pl -c checkcommand -f /root/nems/nems-migrator/data/1.5/nagios/nconf_patches/check_temper.cfg 2>&1 | grep -E "ERROR"
fi
# Upgrade check_temper from to current version
temperver=1.7 # Current version of Temper script
if ! grep -q "Version $temperver" /usr/local/nagios/libexec/check_temper; then
cp -f /root/nems/nems-migrator/data/1.6/nagios/plugins/check_temper /usr/lib/nagios/plugins/check_temper
fi
# check_temper requires access to USB ports as non-root
# Allow this every time (note this runs at every reboot)
if [[ -e /dev/ttyUSB0 ]]; then
chmod a+rw /dev/ttyUSB0
fi
if [[ -e /dev/ttyUSB1 ]]; then
chmod a+rw /dev/ttyUSB1
fi
if [[ -e /dev/ttyUSB2 ]]; then
chmod a+rw /dev/ttyUSB2
fi
if [[ -e /dev/ttyUSB3 ]]; then
chmod a+rw /dev/ttyUSB3
fi
# Upgrade Telegram
telegramver=1.5.6 # Current version of Telegram script
if ! grep -q "VERSION $telegramver" /usr/lib/nagios/plugins/notify-by-telegram.lua; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/notify-by-telegram.lua /usr/lib/nagios/plugins/notify-by-telegram.lua
fi
# Install nems-webhooktest command if not already
if [ ! -f /usr/local/bin/nems-webhooktest ]; then
ln -s /usr/local/share/nems/nems-scripts/webhooktest.sh /usr/local/bin/nems-webhooktest
fi
# Install nems-passwd command if not already
if [ ! -f /usr/local/bin/nems-passwd ]; then
ln -s /usr/local/share/nems/nems-scripts/nems-passwd.sh /usr/local/bin/nems-passwd
fi
# Install mrtgsetup command if not already
if [ ! -f /usr/local/bin/mrtgsetup ]; then
ln -s /usr/local/share/nems/nems-scripts/mrtg.sh /usr/local/bin/mrtgsetup
fi
# Move NEMS TV Dashboard out of nems-www
if [[ ! -d /var/www/nems-tv ]]; then
cd /var/www
# Obtain nems-tv
git clone https://github.com/Cat5TV/nems-tv
# If the clone was successful, enable nems-tv
if [[ -d /var/www/nems-tv ]]; then
# Add the apache2 conf
cp -f /var/www/nems-tv/nems-tv.conf /etc/apache2/conf-available/
# Set permissions
chown -R www-data:www-data /var/www/nems-tv
chown root:root /etc/apache2/conf-available/nems-tv.conf
# Update nems-www to remove the /tv folder
cd /var/www/html
git pull
# Enable nems-tv
/usr/sbin/a2enconf nems-tv
# Reload apache2
/bin/systemctl reload apache2
fi
fi
# ensure, outside of the download, that the installation was successful
if [[ ! -e /etc/apache2/conf-enabled/nems-tv.conf ]]; then
# Enable nems-tv
/usr/sbin/a2enconf nems-tv
# Reload apache2
/bin/systemctl reload apache2
fi
# Upgrade check_speedtest
if ! grep -q "NEMS00003" /usr/local/nagios/libexec/check_speedtest-cli.sh; then
cp /root/nems/nems-migrator/data/1.6/nagios/plugins/check_speedtest-cli.sh /usr/local/nagios/libexec/
fi
# Add nems-install command
if [[ ! -e /usr/local/bin/nems-install ]]; then
ln -s /usr/local/share/nems/nems-scripts/installers/install-vim3.sh /usr/local/bin/nems-install
fi
# Increase upload size for background images
if [[ -e /etc/php/7.3/phpdbg/php.ini ]]; then
reloadapache=0
if ! grep -q "NEMS00001" /etc/php/7.3/phpdbg/php.ini; then
/bin/sed -i '/post_max_size =/c\; NEMS00001\npost_max_size = 20M' /etc/php/7.3/phpdbg/php.ini
/bin/sed -i '/upload_max_filesize =/c\upload_max_filesize = 16M' /etc/php/7.3/phpdbg/php.ini
reloadapache=1
fi
fi
if [[ -e /etc/php/7.2/phpdbg/php.ini ]]; then
if ! grep -q "NEMS00001" /etc/php/7.2/phpdbg/php.ini; then
/bin/sed -i '/post_max_size =/c\; NEMS00001\npost_max_size = 20M' /etc/php/7.2/phpdbg/php.ini
/bin/sed -i '/upload_max_filesize =/c\upload_max_filesize = 16M' /etc/php/7.2/phpdbg/php.ini
reloadapache=1
fi
if [[ $reloadapache == 1 ]]; then
/bin/systemctl reload apache2
fi
fi
# Give Adagios access to socket
/bin/sed -i -- 's,livestatus_path = None,livestatus_path = "/usr/local/nagios/var/rw/live.sock",g' /var/www/adagios/settings.py
# Ensure ownership of nems-www is set to the apache2 user
chown -R www-data:www-data /var/www/html
# Fix logs for Nagios (in particular, this fixes Adagios history)
if [[ ! -d /var/log/nagios/archives ]]; then
mkdir /var/log/nagios/archives
chown nagios:nagios /var/log/nagios/archives
chmod ug+x /var/log/nagios/archives
chmod g+ws /var/log/nagios/archives
fi
# Allow Nagios to check for external commands from Nagios Core web UI or Adagios
if grep -q "check_external_commands=0" /usr/local/nagios/etc/nagios.cfg; then
/bin/sed -i -- 's/check_external_commands=0/check_external_commands=1/g' /usr/local/nagios/etc/nagios.cfg
/bin/systemctl restart nagios
fi
# Make Adagios work on NEMS 1.4.1+
if [[ -d /var/www/adagios ]]; then
rm -rf /var/www/adagios
ln -s /usr/local/lib/python2.7/dist-packages/adagios /var/www/adagios
fi
if ! grep -q "NEMS00000" /var/www/adagios/settings.py; then
cp -f /root/nems/nems-migrator/data/1.4/adagios/settings.py /var/www/adagios/
chown www-data:www-data /var/www/adagios/settings.py
adagioscache=1;
fi
if ! grep -q "NEMS00000" /var/www/adagios/templates/403.html; then
cp -f /root/nems/nems-migrator/data/1.4/adagios/templates/403.html /var/www/adagios/templates/
adagioscache=1;
fi
if ! grep -q "NEMS00000" /var/www/adagios/templates/base.html; then
cp -f /root/nems/nems-migrator/data/1.4/adagios/templates/base.html /var/www/adagios/templates/
adagioscache=1;
fi
if [[ $adagioscache = "1" ]]; then
cd /var/www/adagios
/usr/bin/find /var/www/adagios/ -name "*.pyc" -exec rm -rf {} \;
/usr/local/bin/pip install --trusted-host pypi.python.org --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host piwheels.org --upgrade pip
/usr/local/bin/pip install --trusted-host pypi.python.org --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host piwheels.org django-clear-cache
/usr/bin/python manage.py clear_cache
/bin/systemctl restart apache2
fi
# Move bootscreen to TTY7 and disable TTY1
if [[ -e /etc/rc.local ]]; then
if ! grep -q "NEMS00001" /etc/rc.local; then
/root/nems/nems-admin/build/010-tty
fi
fi
# Don't output kernel messages -- such as firewall blocks -- to TTY
if [[ -e /etc/rc.local ]]; then
if ! grep -q "NEMS00002" /etc/rc.local; then
/root/nems/nems-admin/build/012-tty
fi
fi
# Allow unauthenticated SMTP
if ! grep -q "NEMS00002" /usr/local/nagios/libexec/nems_sendmail_host; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/nems_sendmail_* /usr/local/nagios/libexec/
fi
# Replace NEMS branding in Cockpit in case an update removes it
/root/nems/nems-admin/build/171-cockpit
/root/nems/nems-admin/build/999-cleanup
# Install glances command
if [[ ! -e /usr/bin/glances ]]; then
apt-get -y install glances
fi
# Mark filesystem as resized if greater than 9 GB, just in case the patch didn't get logged or is virtual appliance (etc)
size=$(df --output=target,size /root | awk ' NR==2 { print $2 } ')
if (( $size > 9000000 )); then
# Log that patch (resize) has been applied to this system
# Activates features such as bootscreen.sh
if ! grep -q "PATCH-000002" /var/log/nems/patches.log; then
echo "PATCH-000002" >> /var/log/nems/patches.log
fi
fi
# enable rc.local service if not enabled (ie., Rock64)
if [[ -e /etc/rc.local ]]; then
if [[ ! -e /etc/systemd/system/rc-local.service ]]; then
/root/nems/nems-admin/build/009-rc_local
/root/nems/nems-admin/build/999-cleanup
fi
fi
# Patch check_rpi_temperature to include error handling for when the thermal sensor doesn't exist (ie., VM)
if ! grep -q "PATCH-000006" /var/log/nems/patches.log; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/check_rpi_temperature /usr/lib/nagios/plugins/
echo "PATCH-000006" >> /var/log/nems/patches.log
fi
# Add some error handling if nems-mailtest was run as a non-root user
if ! grep -q "if (!@file_put_contents('/var/log/nagios/nagios.log'" /usr/local/nagios/libexec/nems_sendmail_service; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/nems_sendmail_service /usr/local/nagios/libexec/
fi
if ! grep -q "if (!@file_put_contents('/var/log/nagios/nagios.log'" /usr/local/nagios/libexec/nems_sendmail_host; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/nems_sendmail_host /usr/local/nagios/libexec/
fi
# Forcibly disable TLS if disabled in NEMS SST
if ! grep -q "SMTPAutoTLS" /usr/local/nagios/libexec/nems_sendmail_service; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/nems_sendmail_service /usr/local/nagios/libexec/
fi
if ! grep -q "SMTPAutoTLS" /usr/local/nagios/libexec/nems_sendmail_host; then
cp -f /root/nems/nems-migrator/data/1.5/nagios/plugins/nems_sendmail_host /usr/local/nagios/libexec/
fi
# Fix issues from early Armbian-based Build Bases
if [[ -d /usr/lib/armbian ]]; then
# Stop /tmp and /var/log from being loaded in zram
sed -i '/\/tmp/d' /etc/fstab
umount /tmp 2>&1
systemctl stop armbian-zram-config
systemctl disable armbian-zram-config
systemctl stop armbian-ramlog
systemctl disable armbian-ramlog
# Delete the services
rm -f /etc/systemd/system/sysinit.target.wants/armbian-*
# Remove Armbian-specific stuff
rm -rf /usr/lib/armbian
rm -rf /usr/share/armbian
rm -f /boot/armbian_first_run*
apt-get -y remove --purge armbian-config
# Change name of armbianEnv file to bootEnv
mv /boot/armbianEnv.txt /boot/bootEnv.txt
sed -i 's/armbianEnv/bootEnv/g' /boot/boot.cmd
reboot=1
fi
# Backport speedtest from 1.6 to 1.4.1+ (essentially, for 1.5).
if ! grep -q "PATCH-000017" /var/log/nems/patches.log; then
if [[ -e /usr/lib/nagios/plugins/check_speedtest-cli.sh ]]; then
rm /usr/lib/nagios/plugins/check_speedtest-cli.sh
fi
wget -O /usr/lib/nagios/plugins/check_speedtest-cli.sh https://raw.githubusercontent.com/NEMSLinux/nems-plugins/main/debpack/usr/lib/nagios/plugins/check_speedtest-cli.sh
chmod +x /usr/lib/nagios/plugins/check_speedtest-cli.sh
if [ $(dpkg-query -W -f='${Status}' npm 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
apt-get install -y npm
fi
if [[ ! -e /usr/local/bin/speed-cloudflare-cli ]]; then
npm install -g speed-cloudflare-cli
fi
echo "PATCH-000017" >> /var/log/nems/patches.log
fi
fi
if (( $(awk 'BEGIN {print ("'$ver'" <= "'1.3.1'")}') )); then
/usr/local/share/nems/nems-scripts/fixes-legacy.sh
fi
# Install nems-upgrade command if not already
if [ ! -f /usr/local/bin/nems-upgrade ]; then
ln -s /usr/local/share/nems/nems-scripts/upgrade.sh /usr/local/bin/nems-upgrade
fi
# Install nems-update command if not already
if [ ! -f /usr/local/bin/nems-update ]; then
ln -s /usr/local/share/nems/nems-scripts/update.sh /usr/local/bin/nems-update
fi
# Install nems-info command if not already
if [ ! -f /usr/local/bin/nems-info ]; then
ln -s /usr/local/share/nems/nems-scripts/info.sh /usr/local/bin/nems-info
fi
# Install nems-cert command
if [ ! -f /usr/local/bin/nems-cert ]; then
ln -s /usr/local/share/nems/nems-scripts/gen-cert.sh /usr/local/bin/nems-cert
fi
# Install nems-quickfix command
if [ ! -f /usr/local/bin/nems-quickfix ]; then
ln -s /usr/local/share/nems/nems-scripts/quickfix.sh /usr/local/bin/nems-quickfix
fi
# Install nems-support command
if [ ! -f /usr/local/bin/nems-support ]; then
ln -s /root/nems/nems-migrator/support.sh /usr/local/bin/nems-support
fi
# Add new cron entries
# Dump current crontab to tmp file
crontab -l > /tmp/cron.tmp
# Benchmark log
if ! grep -q "NEMS0001" /tmp/cron.tmp; then
printf "\n# Run a weekly system benchmark of the NEMS server to assist with troubleshooting NEMS0001\n0 3 * * 0 /usr/local/share/nems/nems-scripts/benchmark.sh > /var/log/nems/benchmark.log\n" >> /tmp/cron.tmp
cronupdate=1
fi
# NEMS Anonymous Stats
if ! grep -q "NEMS0002" /tmp/cron.tmp; then
printf "\n# NEMS Anonymous Stats NEMS0002\n0 0 * * * /usr/local/share/nems/nems-scripts/stats.sh\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0003" /tmp/cron.tmp; then
printf "\n# Load Average Over One Week Logger NEMS0003\n*/15 * * * * /usr/local/share/nems/nems-scripts/loadlogger.sh cron\n" >> /tmp/cron.tmp
cronupdate=1
fi
# Fix first-gen NEMS0003
if ! grep -q "loadlogger.sh cron" /tmp/cron.tmp; then
/bin/sed -i -- 's/loadlogger.sh/loadlogger.sh cron/g' /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0004" /tmp/cron.tmp; then
printf "\n# Detect Hardware Model NEMS0004\n@reboot /usr/local/share/nems/nems-scripts/hw_model.sh\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0005" /tmp/cron.tmp; then
printf "\n# Log Package Version Info NEMS0005\n0 5 * * 0 /usr/local/share/nems/nems-scripts/versions.sh > /var/log/nems/package-versions.log\n" >> /tmp/cron.tmp
cronupdate=1
fi
# Change nems-update cronjob to nems-quickfix, cutting patch delivery time in half!
if grep -q "/update.sh" /tmp/cron.tmp; then
/bin/sed -i -- 's,/update.sh,/quickfix.sh > /dev/null 2\>\&1,g' /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0006" /tmp/cron.tmp; then
printf "\n# Log CPU Temperature NEMS0006\n*/15 * * * * /usr/local/share/nems/nems-scripts/thermallogger.sh cron\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0007" /tmp/cron.tmp; then
# 1.6 - Now runs random time within 4 hours of cron trigger, so setting to midnight means it will run sometime between midnight and 4am
printf "\n# Run NEMS Migrator Off-Site Backup NEMS0007\n0 0 * * * /root/nems/nems-migrator/offsite-backup.sh > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
else # Move previous versions to midnight
# 1.4
if grep -q "0 4 \* \* \* /root/nems/nems-migrator/offsite-backup.sh" /tmp/cron.tmp; then
/bin/sed -i -- 's,0 4 \* \* \* /root/nems/nems-migrator/offsite-backup.sh,30 23 * * * /root/nems/nems-migrator/offsite-backup.sh,g' /tmp/cron.tmp
cronupdate=1
fi
# 1.5
if grep -q "30 23 \* \* \* /root/nems/nems-migrator/offsite-backup.sh" /tmp/cron.tmp; then
/bin/sed -i -- 's,30 23 \* \* \* /root/nems/nems-migrator/offsite-backup.sh,0 0 * * * /root/nems/nems-migrator/offsite-backup.sh,g' /tmp/cron.tmp
cronupdate=1
fi
fi
if ! grep -q "NEMS0008" /tmp/cron.tmp; then
printf "\n# Log NEMS Migrator Off-Site Backup Stats NEMS0008\n30 4 * * * /usr/local/share/nems/nems-scripts/osb-stats.sh > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0009" /tmp/cron.tmp; then
printf "\n# Test Memory for Errors Weekly NEMS0009\n0 3 * * 0 /usr/sbin/memtester 500 10 > /var/log/nems/memtest.log\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0010" /tmp/cron.tmp; then
printf "\n# Detect and Set Local DNS Settings NEMS0010\n@reboot /sbin/resolvconf -u > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0011" /tmp/cron.tmp; then
printf "\n# support.nems Self-Destruct NEMS0011\n* * * * * /root/nems/nems-migrator/support-sd.sh > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0012" /tmp/cron.tmp; then
if [[ ! -d /var/log/nems/nems-tools/ ]]; then
mkdir /var/log/nems/nems-tools/
fi
printf "\n# nems-tools warninglight NEMS0012\n@reboot /root/nems/nems-tools/warninglight >> /var/log/nems/nems-tools/warninglight 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
# Run it now
/root/nems/nems-tools/warninglight >> /var/log/nems/nems-tools/warninglight 2>&1 &
fi
if ! grep -q "NEMS0013" /tmp/cron.tmp; then
printf "\n# NEMS Cloud State Update NEMS0013\n* * * * * /usr/local/share/nems/nems-scripts/cloud.sh > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0014" /tmp/cron.tmp; then
printf "\n# NEMS Checkin NEMS0014\n*/5 * * * * /usr/local/share/nems/nems-scripts/checkin.sh > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
# Install piWatcher daemon, will load on boot if hat present. Otherwise, will do nothing.
if ! grep -q "NEMS0015" /tmp/cron.tmp; then
if (( $platform < 10 )); then
printf "\n# piWatcher NEMS0015\n@reboot /root/nems/nems-tools/piwatcher > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
fi
# Install the NEMS Tools GPIO Extender daemon.
if ! grep -q "NEMS0016" /tmp/cron.tmp; then
printf "\n# NEMS Tools GPIO Extender Server NEMS0016\n@reboot /root/nems/nems-tools/gpio-extender/gpioe-server > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
# Run it
restartwarninglight=1
/root/nems/nems-tools/gpio-extender/gpioe-server > /dev/null 2>&1 &
fi
if ! grep -q "NEMS0017" /tmp/cron.tmp; then
printf "\n# Enable non-root access to TEMPer NEMS0017\n@reboot /usr/local/share/nems/nems-scripts/temperinit > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
# Run it
/usr/local/share/nems/nems-scripts/temperinit
fi
# Install PiVoyager daemon, will load on boot if hat present. Otherwise, will do nothing.
if ! grep -q "NEMS0018" /tmp/cron.tmp; then
if (( $platform < 10 )); then
printf "\n# PiVoyager NEMS0018\n@reboot /root/nems/nems-tools/pivoyager > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
fi
if ! grep -q "NEMS0019" /tmp/cron.tmp; then
# Install it via patch
if ! grep -q "PATCH-000012" /var/log/nems/patches.log; then
/root/nems/nems-admin/nems-upgrade/patches/000012
fi
printf "\n# Run MRTG Every 5 Minutes NEMS0019\n*/5 * * * * /usr/local/share/nems/nems-scripts/mrtg-gen.sh > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0020" /tmp/cron.tmp; then
printf "\n# Set permissions for USB access (TEMPer) NEMS0020\n*/1 * * * * /root/nems/nems-admin/build/500-temper > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
if ! grep -q "NEMS0021" /tmp/cron.tmp; then
printf "\n# Detect NEMS Server local IP on same Subnet and set NEMS Tools config to point to it NEMS0021\n@reboot /root/nems/nems-tools/detect > /dev/null 2>&1\n" >> /tmp/cron.tmp
cronupdate=1
fi
# Import revised crontab
if [[ "$cronupdate" == "1" ]]
then
crontab /tmp/cron.tmp
fi
# Remove temp file
rm /tmp/cron.tmp
# /Add new cron entries
# Update apt Lists
apt-get update
# Remove apikey if it is not set (eg., did not get a response from the server)
apikey=$(cat /usr/local/share/nems/nems.conf | grep apikey | printf '%s' $(cut -n -d '=' -f 2))
if [[ $apikey == '' ]]; then
/bin/sed -i~ '/apikey/d' /usr/local/share/nems/nems.conf
fi
# Randomize nemsadmin password if NEMS is initialized
# After nems-init you must first become root, then su nemsadmin to access nemsadmin
if [ -d /home/nemsadmin ]; then # the nemsadmin user folder exists
usercount=$(find /home/* -maxdepth 0 -type d | wc -l)
if (( $usercount > 1)); then # Only do this if there are other users on the system
# This assumes (and rightly so) that an extra user means NEMS has been initialized
# It would be best and most accurate to also/instead check the init status
rndpass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1)
echo -e "$rndpass\n$rndpass" | passwd nemsadmin >/tmp/init 2>&1
# Attempt to delete the nemsadmin user: will not proceed if in use
# Doing this now instead of during init to avoid crashing during init
# where user continues as nemsadmin
userdel -r nemsadmin
fi
fi
# Detect Platform if failed previously
if [ ! -f /var/log/nems/hw_model ]; then
/usr/local/share/nems/nems-scripts/hw_model.sh
fi
# Load ZRAM Swap at boot
if [[ -e /etc/rc.local ]]; then
if ! grep -q "NEMS0000" /etc/rc.local; then
# fix comment so it doesn't get replaced
/bin/sed -i -- 's,"exit 0",exit with errorcode 0,g' /etc/rc.local
# add to boot process
/bin/sed -i -- 's,exit 0,# Load Swap into ZRAM NEMS0000\n/usr/local/share/nems/nems-scripts/zram.sh > /dev/null 2>\&1\n\nexit 0,g' /etc/rc.local
# run it now
/usr/local/share/nems/nems-scripts/zram.sh # Do it now
fi
fi
if [ $(dpkg-query -W -f='${Status}' memtester 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
apt-get -y install memtester
fi
# Final cleanup
/root/nems/nems-admin/build/999-cleanup
rm -f /var/run/nems-fixes.pid
# If a reboot is required, do it now
if [[ $reboot == 1 ]]; then
/sbin/reboot
fi