/
RaspiQuery.java
1255 lines (1199 loc) · 49.5 KB
/
RaspiQuery.java
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2016 RasPi Check Contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package de.eidottermihi.rpicheck.ssh.impl;
import com.google.common.base.CharMatcher;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import net.schmizz.sshj.AndroidConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.ConnectionException;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.userauth.UserAuthException;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
import net.schmizz.sshj.xfer.FileSystemFile;
import net.schmizz.sshj.xfer.scp.SCPFileTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.security.Security;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.eidottermihi.rpicheck.ssh.IQueryService;
import de.eidottermihi.rpicheck.ssh.LoadAveragePeriod;
import de.eidottermihi.rpicheck.ssh.beans.DiskUsageBean;
import de.eidottermihi.rpicheck.ssh.beans.NetworkInterfaceInformation;
import de.eidottermihi.rpicheck.ssh.beans.ProcessBean;
import de.eidottermihi.rpicheck.ssh.beans.RaspiMemoryBean;
import de.eidottermihi.rpicheck.ssh.beans.VcgencmdBean;
import de.eidottermihi.rpicheck.ssh.beans.WlanBean;
/**
* Simple API for querying a Raspberry Pi computer.
*
* @author Michael
* @version <ul>
* <li>0.4.0 added sending commands</li>
* <li>0.3.3 added private/public key authentification</li>
* <li>0.3.2 fixed wlan parsing</li>
* <li>0.3.1 added halt commando, general bugfixing of path problems</li>
* <li>0.3.0 added mechanism to find vcgencmd path, refactored ip
* adress query to support other interfaces than eth0, added wlan
* status query</li>
* <li>0.2.1 added full path to vcgencmd (not in PATH in some
* distributions)</li>
* <li>0.2.0 reboot command</li>
* <li>0.1.5 beans serializable</li>
* <li>0.1.4 added process query</li>
* <li>0.1.3 minor changes (avg load as %, show data unit)</li>
* <li>0.1.2 added distribution name query</li>
* <li>0.1.1 added ip adress and disk usage query, changed UptimeBean
* avgLoad to String</li>
* <li>0.1.0 first alpha</li>
* </ul>
*/
public class RaspiQuery implements IQueryService {
private static final Logger LOGGER = LoggerFactory
.getLogger(RaspiQuery.class);
public static final int FREQ_ARM = 0;
public static final int FREQ_CORE = 1;
private static final Pattern IPADDRESS_PATTERN = Pattern
.compile("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
private static final Pattern CPU_PATTERN = Pattern.compile("[0-9.]{4,}");
private static final String DISK_USAGE_CMD = "LC_ALL=C df -h";
private static final String DF_COMMAND_HEADER_START = "Filesystem";
private static final String DISTRIBUTION_CMD = "cat /etc/*-release | grep PRETTY_NAME";
private static final String PROCESS_NO_ROOT_CMD = "ps -U root -u root -N";
private static final String PROCESS_ALL = "ps -A";
private static final String N_A = "n/a";
private static final int DEFAULT_SSH_PORT = 22;
private SSHClient client;
private String hostname;
private String username;
private int port = DEFAULT_SSH_PORT;
static {
//Security.removeProvider("BC");
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}
/**
* Initialize a new RaspiQuery.
*
* @param host hostname or ip adress of a running raspberry pi
* @param user username for ssh login
* @param port ssh port to use (if null, default will be used)
*/
public RaspiQuery(final String host, final String user, final Integer port) {
if (Strings.isNullOrEmpty(host)) {
throw new IllegalArgumentException("hostname should not be blank.");
} else if (Strings.isNullOrEmpty(user)) {
throw new IllegalArgumentException("username should not be blank.");
} else {
LOGGER.info("New RaspiQuery for host: {}", host);
this.hostname = host;
this.username = user;
if (port != null) {
this.port = port;
}
}
}
/**
* Queries the current CPU temperature.
*
* @param vcgencmdPath the path to vcgencmd
* @return the temperature in Celsius
* @throws RaspiQueryException if something goes wrong
*/
private final Double queryCpuTemp(String vcgencmdPath)
throws RaspiQueryException {
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final String cmdString = vcgencmdPath + " measure_temp";
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream())
.toString();
return this.parseTemperature(output);
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/**
* Queries the current cpu frequency.
*
* @param unit cpu or arm
* @param vcgencmdPath the path of the vcgendcmd tool
* @return the frequency in hz
* @throws RaspiQueryException if something goes wrong
*/
private final long queryFreq(final int unit, final String vcgencmdPath)
throws RaspiQueryException {
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
String cmdString = vcgencmdPath + " measure_clock";
if (unit == FREQ_ARM) {
cmdString += " arm";
} else if (unit == FREQ_CORE) {
cmdString += " core";
} else {
return 0;
}
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream())
.toString();
return this.parseFrequency(output);
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryVcgencmd()
*/
@Override
public final VcgencmdBean queryVcgencmd() throws RaspiQueryException {
LOGGER.debug("Querying vcgencmd...");
// first, find the location of vcgencmd
final String vcgencmdPath = findVcgencmd().orNull();
if (vcgencmdPath == null) {
throw RaspiQueryException.createVcgencmdNotFound();
}
// create Bean
final VcgencmdBean bean = new VcgencmdBean();
bean.setArmFrequency(this.queryFreq(FREQ_ARM, vcgencmdPath));
bean.setCoreFrequency(this.queryFreq(FREQ_CORE, vcgencmdPath));
bean.setCoreVolts(this.queryVolts(vcgencmdPath));
bean.setCpuTemperature(this.queryCpuTemp(vcgencmdPath));
bean.setVersion(this.queryFirmwareVersion(vcgencmdPath));
return bean;
}
/**
* Queries the firmware version.
*
* @param vcgencmdPath path to vcgencmd
* @return the firmware Version
* @throws RaspiQueryException if something goes wrong
*/
public String queryFirmwareVersion(String vcgencmdPath)
throws RaspiQueryException {
return QueryFactory.makeFirmwareQuery(client, vcgencmdPath).run();
}
/**
* Checks the known paths to the vcgencmd executable and return the first
* path found.
*
* @return the path or <code>null</code>, when vcgencmd was not found
* @throws RaspiQueryException if something goes wrong
*/
private Optional<String> findVcgencmd() throws RaspiQueryException {
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
try {
final String[] pathsToCheck = new String[]{"vcgencmd", "/usr/bin/vcgencmd", "/opt/vc/bin/vcgencmd"};
String foundPath = null;
for (int i = 0; i < pathsToCheck.length; i++) {
String guessedPath = pathsToCheck[i];
if (isValidVcgencmdPath(guessedPath, client)) {
foundPath = guessedPath;
break;
}
}
if (foundPath != null) {
LOGGER.info("Found vcgencmd in path: {}.", foundPath);
return Optional.of(foundPath);
} else {
LOGGER.error("vcgencmd was not found. Verify that vcgencmd is available in /usr/bin or /opt/vc/bin and make sure your user is in group 'video'.");
return Optional.absent();
}
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/**
* Checks if the path is a correct path to vcgencmd.
*
* @param path the path to check
* @param client authenticated and open client
* @return true, if correct, false if not
* @throws IOException if something ssh related goes wrong
*/
private boolean isValidVcgencmdPath(String path, SSHClient client) throws IOException {
final Session session = client.startSession();
session.allocateDefaultPTY();
LOGGER.debug("Checking vcgencmd location: {}", path);
final Command cmd = session.exec(path);
cmd.join(30, TimeUnit.SECONDS);
session.close();
final Integer exitStatus = cmd.getExitStatus();
final String output = IOUtils.readFully(cmd.getInputStream()).toString().toLowerCase();
LOGGER.debug("Path check output: {}", output);
if (exitStatus != null && exitStatus.equals(0)
&& !output.contains("not found") && !output.contains("no such file or directory")) {
// vcgencmd was found
return true;
} else {
return false;
}
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryNetworkInformation()
*/
@Override
public List<NetworkInterfaceInformation> queryNetworkInformation()
throws RaspiQueryException {
List<NetworkInterfaceInformation> interfacesInfo = new ArrayList<NetworkInterfaceInformation>();
// 1. find all network interfaces (excluding loopback interface) and
// check carrier
List<String> interfaces = this.queryInterfaceList();
LOGGER.info("Available interfaces: {}", interfaces);
for (String interfaceName : interfaces) {
NetworkInterfaceInformation interfaceInfo = new NetworkInterfaceInformation();
interfaceInfo.setName(interfaceName);
// check carrier
interfaceInfo.setHasCarrier(this.checkCarrier(interfaceName));
interfacesInfo.add(interfaceInfo);
}
List<NetworkInterfaceInformation> wirelessInterfaces = new ArrayList<NetworkInterfaceInformation>();
// 2. for every interface with carrier check ip adress
for (NetworkInterfaceInformation interfaceBean : interfacesInfo) {
if (interfaceBean.isHasCarrier()) {
interfaceBean.setIpAdress(this.queryIpAddress(interfaceBean
.getName()));
// check if interface is wireless (interface name starts with
// "wlan")
if (interfaceBean.getName().startsWith("wlan")) {
// add to wireless interfaces list
wirelessInterfaces.add(interfaceBean);
}
}
}
// 3. query signal level and link status of wireless interfaces
if (wirelessInterfaces.size() > 0) {
this.queryWlanInfo(wirelessInterfaces);
}
return interfacesInfo;
}
/**
* Queries the link level and signal quality of the wireless interfaces via
* "cat /proc/net/wireless".
*
* @param wirelessInterfaces a List with wireless interfaces
* @throws RaspiQueryException if something goes wrong
*/
private void queryWlanInfo(
List<NetworkInterfaceInformation> wirelessInterfaces)
throws RaspiQueryException {
LOGGER.info("Querying wireless interfaces...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final String cmdString = "cat /proc/net/wireless";
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
String output = IOUtils.readFully(cmd.getInputStream())
.toString();
LOGGER.debug("Real output of /proc/net/wireless: \n{}",
output);
this.parseWlan(output, wirelessInterfaces);
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
private void parseWlan(String output,
List<NetworkInterfaceInformation> wirelessInterfaces) {
final String[] lines = output.split("\n");
for (String line : lines) {
if (line.startsWith("Inter-") || line.startsWith(" face")) {
LOGGER.debug("Skipping header line: {}", line);
continue;
}
final String[] cols = line.split("\\s+");
if (cols.length >= 11) {
LOGGER.debug("Parsing output line: {}", line);
// getting interface name
final String name = cols[1].replace(":", "");
LOGGER.debug("Parsed interface name: {}", name);
final String linkQuality = cols[3].replace(".", "");
LOGGER.debug("LINK QUALITY>>>{}<<<", linkQuality);
final String linkLevel = cols[4].replace(".", "");
LOGGER.debug("LINK LEVEL>>>{}<<<", linkLevel);
Integer linkQualityInt = null;
try {
linkQualityInt = Integer.parseInt(linkQuality);
} catch (NumberFormatException e) {
LOGGER.warn(
"Could not parse link quality field for input: {}.",
linkQuality);
}
Integer signalLevelInt = null;
try {
signalLevelInt = Integer.parseInt(linkLevel);
} catch (Exception e) {
LOGGER.warn(
"Could not parse link level field for input: {}.",
linkLevel);
}
LOGGER.debug(
"WLAN status of {}: link quality {}, signal level {}.",
new Object[]{name, linkQualityInt, signalLevelInt});
for (NetworkInterfaceInformation iface : wirelessInterfaces) {
if (iface.getName().equals(name)) {
final WlanBean wlanInfo = new WlanBean();
wlanInfo.setLinkQuality(linkQualityInt);
wlanInfo.setSignalLevel(signalLevelInt);
LOGGER.debug(
"Adding wifi-status info to interface {}.",
iface.getName());
iface.setWlanInfo(wlanInfo);
}
}
}
}
}
/**
* Queries the ip address of a network interface.
*
* @param name the interface name (eth0, wlan0, ...).
* @return the IP address
* @throws RaspiQueryException
*/
private String queryIpAddress(String name) throws RaspiQueryException {
LOGGER.info("Querying ip address of interface: '{}'", name);
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
session.allocateDefaultPTY();
final String cmdString = "ip -f inet addr show dev " + name
+ " | sed -n 2p";
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
final String output = IOUtils.readFully(
cmd.getInputStream()).toString();
LOGGER.debug("Output of ip query: {}", output);
final Matcher m = IPADDRESS_PATTERN.matcher(output);
if (m.find()) {
final String ipAddress = m.group().trim();
LOGGER.info("{} - IP address: {}.", name, ipAddress);
return ipAddress;
} else {
LOGGER.error(
"IP address pattern: No match found for output: {}.",
output);
}
LOGGER.info(
"'ip' command not available. Trying '/sbin/ifconfig' to get ip address of interface {}.",
name);
session = client.startSession();
session.allocateDefaultPTY();
final String ifConfigCmd = "/sbin/ifconfig " + name
+ " | grep \"inet addr\"";
final Command ifCfgCmd = session.exec(ifConfigCmd);
ifCfgCmd.join(30, TimeUnit.SECONDS);
final String ifconfigOutput = IOUtils.readFully(
ifCfgCmd.getInputStream()).toString();
LOGGER.debug("Output of ifconfig query: {}.",
ifconfigOutput);
final Matcher m2 = IPADDRESS_PATTERN
.matcher(ifconfigOutput);
if (m2.find()) {
final String ipAddress = m2.group().trim();
LOGGER.info("{} - IP address: {}.", name, ipAddress);
return ipAddress;
} else {
LOGGER.error(
"IP address pattern: No match found for output: {}.",
ifconfigOutput);
LOGGER.error("Querying ip address failed. It seems like 'ip' and 'ifconfig' are not available.");
return null;
}
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/**
* Checks if the specified interface has a carrier up and running via
* "cat /sys/class/net/[interface]/carrier".
*
* @param interfaceName the interface to check
* @return true, when the interface has a carrier up and running
* @throws RaspiQueryException if something goes wrong
*/
private boolean checkCarrier(String interfaceName)
throws RaspiQueryException {
LOGGER.info("Checking carrier of {}...", interfaceName);
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final String cmdString = "cat /sys/class/net/"
+ interfaceName + "/carrier";
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
final String output = IOUtils.readFully(
cmd.getInputStream()).toString();
if (output.contains("1")) {
LOGGER.debug("{} has a carrier up and running.",
interfaceName);
return true;
} else {
LOGGER.debug("{} has no carrier.", interfaceName);
return false;
}
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/**
* Queries which interfaces are available via "/sys/class/net". Loopback
* interfaces are excluded.
*
* @return a List with all interface names (eth0, wlan0,...).
* @throws RaspiQueryException if something goes wrong
*/
private List<String> queryInterfaceList() throws RaspiQueryException {
LOGGER.info("Querying network interfaces...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final String cmdString = "ls -1 /sys/class/net";
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
final String output = IOUtils.readFully(
cmd.getInputStream()).toString();
final String[] lines = output.split("\n");
final List<String> interfaces = new ArrayList<String>();
for (String interfaceLine : lines) {
if (!interfaceLine.startsWith("lo")) {
LOGGER.debug("Found interface {}.", interfaceLine);
interfaces.add(interfaceLine);
}
}
return interfaces;
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see
* de.eidottermihi.rpicheck.ssh.IQueryService#queryVolts(java.lang.String)
*/
@Override
public final Double queryVolts(String vcgencmdPath)
throws RaspiQueryException {
LOGGER.info("Querying core volts...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final String cmdString = vcgencmdPath
+ " measure_volts core";
final Command cmd = session.exec(cmdString);
cmd.join(30, TimeUnit.SECONDS);
final String output = IOUtils.readFully(
cmd.getInputStream()).toString();
return this.formatVolts(output);
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryUptime()
*/
@Override
public final double queryUptime() throws RaspiQueryException {
return QueryFactory.makeUptimeQuery(client).run();
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryCpuSerial()
*/
@Override
public final String queryCpuSerial() throws RaspiQueryException {
return QueryFactory.makeSerialNoQuery(client).run();
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryMemoryInformation()
*/
@Override
public final RaspiMemoryBean queryMemoryInformation()
throws RaspiQueryException {
return QueryFactory.makeMemoryQuery(client).run();
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryDiskUsage()
*/
@Override
public final List<DiskUsageBean> queryDiskUsage()
throws RaspiQueryException {
LOGGER.info("Querying disk usage...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final Command cmd = session.exec(DISK_USAGE_CMD);
cmd.join(30, TimeUnit.SECONDS);
return this.parseDiskUsage(IOUtils
.readFully(cmd.getInputStream()).toString().trim());
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryDistributionName()
*/
@Override
public final String queryDistributionName() throws RaspiQueryException {
LOGGER.info("Querying distribution name...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final Command cmd = session.exec(DISTRIBUTION_CMD);
cmd.join(30, TimeUnit.SECONDS);
return this.parseDistribution(IOUtils
.readFully(cmd.getInputStream()).toString().trim());
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see de.eidottermihi.rpicheck.ssh.IQueryService#queryProcesses(boolean)
*/
@Override
public final List<ProcessBean> queryProcesses(boolean showRootProcesses)
throws RaspiQueryException {
LOGGER.info("Querying running processes...");
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
final Command cmd = session
.exec(showRootProcesses ? PROCESS_ALL
: PROCESS_NO_ROOT_CMD);
cmd.join(30, TimeUnit.SECONDS);
return this.parseProcesses(IOUtils
.readFully(cmd.getInputStream()).toString().trim());
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see
* de.eidottermihi.rpicheck.ssh.IQueryService#sendRebootSignal(java.lang
* .String)
*/
@Override
public final void sendRebootSignal(String sudoPassword)
throws RaspiQueryException {
if (sudoPassword == null) {
LOGGER.info("No sudo password for reboot specified. Using empty password instead.");
sudoPassword = "";
}
final StringBuilder sb = new StringBuilder();
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
session.allocateDefaultPTY();
final String command = sb.append("echo ").append("\"")
.append(sudoPassword).append("\"")
.append(" | sudo -S /sbin/shutdown -r now")
.toString();
final String rebootCmdLogger = "echo \"??SUDO_PW??\" | sudo -S /sbin/shutdown -r now";
LOGGER.info("Sending reboot command: {}", rebootCmdLogger);
Command cmd = session.exec(command);
cmd.join();
session.close();
if (cmd.getExitStatus() != null && cmd.getExitStatus() != 0) {
LOGGER.warn("Sudo unknown: Trying \"reboot\"...");
// openelec running
session = client.startSession();
session.allocateDefaultPTY();
session.exec("reboot");
try {
session.join(250, TimeUnit.MILLISECONDS);
LOGGER.debug("join successful after 'reboot'.");
} catch (ConnectionException e) {
// system went down
LOGGER.debug("'reboot' successful!");
}
}
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/*
* (non-Javadoc)
*
* @see
* de.eidottermihi.rpicheck.ssh.IQueryService#sendHaltSignal(java.lang.String
* )
*/
@Override
public final void sendHaltSignal(String sudoPassword)
throws RaspiQueryException {
if (sudoPassword == null) {
LOGGER.info("No sudo password for halt specified. Using empty password instead.");
sudoPassword = "";
}
final StringBuilder sb = new StringBuilder();
if (client != null) {
if (client.isConnected() && client.isAuthenticated()) {
Session session;
try {
session = client.startSession();
session.allocateDefaultPTY();
final String command = sb.append("echo ").append("\"")
.append(sudoPassword).append("\"")
.append(" | sudo -S /sbin/shutdown -h now")
.toString();
final String haltCmdLogger = "echo \"??SUDO_PW??\" | sudo -S /sbin/shutdown -h now";
LOGGER.info("Sending halt command: {}", haltCmdLogger);
Command cmd = session.exec(command);
cmd.join();
session.close();
if (cmd.getExitStatus() != null && cmd.getExitStatus() != 0) {
// openelec running
session = client.startSession();
session.allocateDefaultPTY();
LOGGER.warn("Sudo unknown: Trying \"halt\"...");
session.exec("halt");
try {
session.join(250, TimeUnit.MILLISECONDS);
LOGGER.debug("'halt' probably didnt work.");
} catch (ConnectionException e) {
// system went down
LOGGER.debug("'halt' successful!");
}
}
} catch (IOException e) {
throw RaspiQueryException.createTransportFailure(hostname,
e);
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
} else {
throw new IllegalStateException(
"You must establish a connection first.");
}
}
/**
* Parses the output of the ps command.
*
* @param output
* @return List with processes
*/
private List<ProcessBean> parseProcesses(String output) {
final List<String> lines = Splitter.on("\n").trimResults()
.splitToList(output);
final List<ProcessBean> processes = new LinkedList<ProcessBean>();
int count = 0;
for (String line : lines) {
if (count == 0) {
// first line
count++;
continue;
}
// split line at whitespaces
final List<String> cols = Splitter.on(CharMatcher.WHITESPACE)
.omitEmptyStrings().trimResults().splitToList(line);
if (cols.size() >= 4) {
try {
// command may contain whitespace, so join again
final StringBuilder sb = new StringBuilder();
for (int i = 3; i < cols.size(); i++) {
sb.append(cols.get(i)).append(' ');
}
processes.add(new ProcessBean(
Integer.parseInt(cols.get(0)), cols.get(1), cols
.get(2), sb.toString()));
} catch (NumberFormatException e) {
LOGGER.error("Could not parse processes.");
LOGGER.error("Error occured on following line: {}", line);
}
} else {
LOGGER.error("Line[] length: {}", cols.size());
LOGGER.error(
"Expcected another output of ps. Skipping line: {}",
line);
}
}
return processes;
}
private String parseDistribution(String output) {
final String[] split = output.trim().split("=");
if (split.length >= 2) {
final String distriWithApostroph = split[1];
final String distri = distriWithApostroph.replace("\"", "");
return distri;
} else {
LOGGER.error("Could not parse distribution. Make sure 'cat /etc/*-release' works on your distribution.");
LOGGER.error("Output of {}: \n{}", DISTRIBUTION_CMD, output);
return N_A;
}
}
/**
* Parses the output of the disk usage commando.
*
* @param output the output
* @return a List with {@link DiskUsageBean}
*/
private List<DiskUsageBean> parseDiskUsage(String output) {
final String[] lines = output.split("\n");
final List<DiskUsageBean> disks = new LinkedList<DiskUsageBean>();
for (String line : lines) {
if (line.startsWith(DF_COMMAND_HEADER_START)) {
continue;
}
// split string at whitespaces
final String[] linesSplitted = line.split("\\s+");
if (linesSplitted.length >= 6) {
if (linesSplitted.length > 6) {
// whitespace in mountpoint path
StringBuilder sb = new StringBuilder();
for (int i = 5; i < linesSplitted.length; i++) {
sb.append(linesSplitted[i]);
if (i != linesSplitted.length - 1) {
sb.append(" ");
}
}
disks.add(new DiskUsageBean(linesSplitted[0],
linesSplitted[1], linesSplitted[2],
linesSplitted[3], linesSplitted[4], sb.toString()));
} else {
disks.add(new DiskUsageBean(linesSplitted[0],
linesSplitted[1], linesSplitted[2],
linesSplitted[3], linesSplitted[4],
linesSplitted[5]));
}
} else {
LOGGER.warn(
"Expected another output of df -h. Skipping line: {}",
line);
}
}
return disks;
}
private Double formatVolts(String output) {
final String[] splitted = output.trim().split("=");
if (splitted.length >= 2) {
final String voltsWithUnit = splitted[1];
final String volts = voltsWithUnit.substring(0,
voltsWithUnit.length() - 1);
return Double.parseDouble(volts);
} else {
LOGGER.error("Could not parse cpu voltage.");
LOGGER.error("Output of 'vcgencmd measure_volts core': \n{}",
output);
return 0D;
}
}
/**
* Formats the output of the vcgencmd measure_temp command.
*