Advertisement
Kimarite

lhg-connector: lhg-service (PPA)

Jul 1st, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # regular check of hardware, if new hardware is found data is uploaded
  4. # to Linux-Hardware-Guide database
  5.  
  6. # Currently only checking for new PCI and USB devices
  7.  
  8. $sid = $ARGV[0];
  9. $upload_server="http://library.linux-hardware-guide.com";
  10. $TMPDIR = "$ENV{HOME}/.tmp";
  11.  
  12. # Check for cached uploads - if found upload now
  13. check_missed_uploads();
  14.  
  15. # Flag, if new hardware is found
  16. $found_new_hardware = 0;
  17.  
  18. # check for first run
  19. check_first_run();
  20.  
  21. check_root();
  22. check_spool();
  23.  
  24. # ToDo: check if last upload failed -> $found_new_hardware = 1;
  25.  
  26. create_fingerprint_pci();
  27. create_fingerprint_usb();
  28.  
  29. #print "PCI: \n";
  30. #print "Known: @known_pci_array \n";
  31. #print "Scan: @foundarray_pci";
  32.  
  33. #print "\nUSB: \n";
  34. #print "Known: @known_usb_array \n";
  35. #print "@foundarray_usb";
  36.  
  37. check_for_new_ids();
  38.  
  39. if ($found_new_hardware == 1) {
  40. upload_data();
  41. } else {
  42. print "No new hardware found \n";
  43. }
  44. make_clean();
  45.  
  46. exit 0;
  47.  
  48.  
  49. #
  50. # create_fingerprint_pci
  51. #
  52. # read pci IDs of hardware
  53. #
  54.  
  55. sub create_fingerprint_pci {
  56. # modified by Charles K Barcza <blackPanther OS> because the
  57. #/var/tmp or $ENV{HOME}/tmp better if system use tmpfs!
  58.  
  59. system ("mkdir -p $TMPDIR/.LHG_".$sid);
  60. system ("lspci -nnk > $TMPDIR/.LHG_".$sid."/lspci.txt");
  61.  
  62. open(FILE, "<", "$TMPDIR/.LHG_".$sid."/lspci.txt");
  63. open(FILEN, "<", "$TMPDIR/.LHG_".$sid."/lspci.txt");
  64.  
  65. $nextline=<FILEN>;
  66. $i=0;
  67. #print "PCI Fingerprint: \n";
  68. while ( <FILE> ) {
  69. $nextline = <FILEN>;
  70. #print "Line: $_";
  71.  
  72. $pciid = grab_pciid($_);
  73. if ($pciid != "") {
  74. #print "PCIID: $pciid \n";
  75. $subsystemid = grab_subsystemid($nextline);
  76. #print "Subsystem: $subsystemid \n";
  77. }
  78.  
  79.  
  80.  
  81. if ($pciid != "") {
  82. #print "$pciid,";
  83.  
  84. @foundarray_pci = (@foundarray_pci, $pciid);
  85.  
  86. }
  87.  
  88. #
  89. #
  90. }
  91.  
  92. #print "\n";
  93.  
  94. }
  95.  
  96.  
  97. sub grab_subsystemid {
  98. my $line = shift;
  99.  
  100. #print "-------\nLine: $line ";
  101.  
  102. if ( ($line =~ m/Subsystem/) and ($line =~ m/\w\w\w\w:\w\w\w\w/) ) {
  103. my $pciid = substr($line,$-[0],9);
  104. #print "PCIID: $pciid \n";
  105. return $pciid;
  106. } else {
  107. #print " no match \n";
  108. }
  109.  
  110. }
  111.  
  112. #
  113. #
  114. ### USB subroutines
  115. #
  116. #
  117.  
  118. sub create_fingerprint_usb {
  119.  
  120. system ("lsusb > $TMPDIR/.LHG_".$sid."/lsusb.txt");
  121.  
  122. open(FILE, "<", "$TMPDIR/.LHG_".$sid."/lsusb.txt");
  123.  
  124. $i=0;
  125.  
  126. #print "USB Fingerprint: \n";
  127. while ( <FILE> ) {
  128. #print "Line: $_ \n";
  129.  
  130. $usbid = grab_usbid($_);
  131. #$usbname = grab_usbname($_);
  132.  
  133. if ($usbid ne "") {
  134. #print "$usbid,";
  135. @foundarray_usb = (@foundarray_usb, $usbid);
  136. }
  137.  
  138. $i++;
  139. #print "\n";
  140. }
  141. }
  142.  
  143. sub grab_usbid {
  144. $line = shift;
  145.  
  146. #print "-------\nLine: $line ";
  147.  
  148. if ($line =~ m/\w\w\w\w:\w\w\w\w/) {
  149. return substr($line,$-[0],9);
  150. } else {
  151. #print " no match \n";
  152. }
  153.  
  154. }
  155.  
  156. sub grab_pciid {
  157. $line = shift;
  158.  
  159. #print "-------\nLine: $line ";
  160.  
  161. if ( ($line =~ m/\w\w\w\w:\w\w\w\w/) and !($line =~ m/Subsystem/) ) {
  162. $pciid = substr($line,$-[0],9);
  163. #print "PCIID: $pciid \n";
  164. return $pciid;
  165. } else {
  166. #print " no match \n";
  167. }
  168.  
  169. }
  170.  
  171. sub check_root {
  172.  
  173. if ( $< != 0 ) {
  174. #print "WARNING:\n";
  175. #print "========\n";
  176. print "Sorry! This script needs to be running with root privileges. ";
  177. print "\n";
  178. exit 1;
  179. }
  180. }
  181.  
  182. sub check_spool {
  183.  
  184. if ( -e "/var/spool/lhg-tools/pcidata" ) {
  185. #print "pcidata exists";
  186.  
  187. # read in known data from spool file
  188. @known_pci_array = lhg_readdata("/var/spool/lhg-tools/pcidata");
  189.  
  190. } else {
  191. #print "pcidata does not exist";
  192. $found_new_hardware = 1;
  193. system ("mkdir -p /var/spool/lhg-tools/");
  194. system ("touch /var/spool/lhg-tools/pcidata");
  195. system ('echo "# List of PCI data \n" >> /var/spool/lhg-tools/pcidata');
  196. }
  197.  
  198. if ( -e "/var/spool/lhg-tools/usbdata" ) {
  199. #print "usbdata exists";
  200. @known_usb_array = lhg_readdata("/var/spool/lhg-tools/usbdata");
  201.  
  202. } else {
  203. #print "usbdata does not exist";
  204. $found_new_hardware = 1;
  205. system ("mkdir -p /var/spool/lhg-tools/");
  206. system ("touch /var/spool/lhg-tools/usbdata");
  207. system ('echo "# List of USB data \n" >> /var/spool/lhg-tools/usbdata');
  208.  
  209. # read in known data from spool file
  210.  
  211. }
  212. }
  213.  
  214. sub make_clean {
  215.  
  216. # ToDo: cleanup files
  217. }
  218.  
  219. sub lhg_readdata {
  220.  
  221. $FILE = shift;
  222. @data_array = ();
  223. open(FILE, $FILE);
  224. while (<FILE>) {
  225.  
  226. if (substr($_,0,1) eq "#") {
  227. # skip comment lines
  228.  
  229. } else {
  230. $id = $_;
  231. $id =~ s/(\r)|(\n)//g;
  232. @data_array = (@data_array, $id);
  233. }
  234. }
  235. # ToDo: read data from file
  236.  
  237. return @data_array;
  238. }
  239.  
  240. sub check_for_new_ids {
  241.  
  242. #print "\n";
  243.  
  244. # compare PCI IDs
  245. foreach (@foundarray_pci)
  246. {
  247. $searchfor = $_ ;
  248. #print "checking $searchfor ...";
  249. #if ($_ ~~ @known_pci_array) {
  250. if ( grep( /^${searchfor}/, @known_pci_array ) ) {
  251. #print "found \n";
  252. }else {
  253. #print "not found \n";
  254. $found_new_hardware = 1;
  255. # add to file
  256. system ('echo "'.$searchfor.'" >> /var/spool/lhg-tools/pcidata');
  257.  
  258. }
  259. }
  260.  
  261. # compare USB IDs
  262. foreach (@foundarray_usb)
  263. {
  264. $searchfor = $_ ;
  265. #print "checking $searchfor ...";
  266. #if ($_ ~~ @known_usb_array) {
  267. if ( grep( /^${searchfor}/, @known_usb_array ) ) {
  268. #print "found \n";
  269. }else {
  270. #print "not found \n";
  271. $found_new_hardware = 1;
  272. # add to file
  273. system ('echo "'.$searchfor.'" >> /var/spool/lhg-tools/usbdata');
  274.  
  275. }
  276. }
  277.  
  278. }
  279.  
  280. sub upload_data {
  281.  
  282. my $exit_val;
  283. my $pid;
  284.  
  285. print "found new HW -> starting upload \n";
  286.  
  287. if ( -e "./scan_hardware") {
  288.  
  289. print "executing HW scan \n";
  290.  
  291. $pid = fork;
  292. die "Fork failed: $!" unless defined $pid;
  293. unless ($pid) {
  294. #exec "./long.pl"; # Some long running process.
  295. exec ("./scan_hardware");
  296. }
  297.  
  298.  
  299. }elsif ( -e "/usr/bin/scan_hardware") {
  300.  
  301. print "executing HW scan \n";
  302.  
  303. $pid = fork;
  304. die "Fork failed: $!" unless defined $pid;
  305. unless ($pid) {
  306. #exec "./long.pl"; # Some long running process.
  307. #exec ("./scan_hardware");
  308. exec ("/usr/bin/scan_hardware");
  309. }
  310. }
  311.  
  312. #wait for children to finish
  313. waitpid $pid, 0;
  314. #print "PID was: $pid \n";
  315. $exit_val = $? >> 8;
  316. #print "Exit Val: $exit_val \n";
  317.  
  318. if ($exit_val > 0) {
  319. # upload did not work. store for later
  320. my @chars = (0 .. 9, "A".."Z", "a".."z");
  321. my $rndstring;
  322. $rndstring .= $chars[rand @chars] for 1..8;
  323. system ("mkdir -p /var/spool/lhg-tools/missed_uploads.$rndstring");
  324. system ("cp $TMPDIR/.LHG_$pid/* /var/spool/lhg-tools/missed_uploads.$rndstring/");
  325.  
  326. } else {
  327. # ToDo: upload done, also upload missed files
  328. }
  329. }
  330.  
  331. sub check_first_run {
  332.  
  333. $conf_filename = '/etc/lhg-tools.conf';
  334. $pci_filename = '/var/spool/lhg-tools/pcidata';
  335.  
  336. if ( (-e $conf_filename) && (-e $pci_filename) ){
  337. # files exists, program was run before
  338.  
  339. }else {
  340.  
  341. #enforcing hardware scan
  342. $found_new_hardware = 1;
  343.  
  344. }
  345. }
  346.  
  347. sub check_missed_uploads {
  348.  
  349. @files = glob("/var/spool/lhg-tools/missed_uploads.*");
  350.  
  351. foreach (@files) {
  352. # do something
  353.  
  354. #print "uploading $_ ";
  355.  
  356. upload_cached_files($_);
  357.  
  358. }
  359.  
  360. }
  361.  
  362. sub upload_cached_files {
  363.  
  364. my $TMPDIR = shift;
  365. my $fname = $TMPDIR;;
  366. $fname =~ s/\/var\/spool\/lhg-tools\///;
  367.  
  368. my @allfiles = ("alsa_cards.txt","alsa_devices.txt","aplay.txt","cpuinfo.txt",
  369. "dd.err.txt","dd.txt","dmesg.txt","lsb_release.txt","lspci.txt",
  370. "lsusb.txt","lsusb_v.err.txt","lsusb_v.txt","scanimage.txt",
  371. "version.txt","hdparm.txt","hdparm_direct.txt");
  372.  
  373.  
  374. my $sid;
  375. use LWP::UserAgent;
  376. use HTTP::Request::Common;
  377.  
  378. #get session ID
  379. $request = HTTP::Request->new(GET => $upload_server."/uploadrequest.php?v=0.2");
  380. $ua = LWP::UserAgent->new;
  381. $response = $ua->request($request);
  382. $sid = $response->content;
  383.  
  384. #$ua = LWP::UserAgent->new;
  385. #upload files
  386. $response = $ua->request(POST $upload_server."/uploadfiles.php",
  387.  
  388. Content_Type => 'form-data',
  389.  
  390. Content => [ sid => $sid,
  391. file1 => ["$TMPDIR/".$allfiles[0]],
  392. file2 => ["$TMPDIR/".$allfiles[1]],
  393. file3 => ["$TMPDIR/".$allfiles[2]],
  394. file4 => ["$TMPDIR/".$allfiles[3]],
  395. file5 => ["$TMPDIR/".$allfiles[4]],
  396. file6 => ["$TMPDIR/".$allfiles[5]],
  397. file7 => ["$TMPDIR/".$allfiles[6]],
  398. file8 => ["$TMPDIR/".$allfiles[7]],
  399. file9 => ["$TMPDIR/".$allfiles[8]],
  400. file10 => ["$TMPDIR/".$allfiles[9]],
  401. file11 => ["$TMPDIR/".$allfiles[10]],
  402. file12 => ["$TMPDIR/".$allfiles[11]],
  403. file13 => ["$TMPDIR/".$allfiles[12]],
  404. file14 => ["$TMPDIR/".$allfiles[13]],
  405. file15 => ["$TMPDIR/".$allfiles[14]],
  406. file16 => ["$TMPDIR/".$allfiles[15]],
  407. ]
  408.  
  409. );
  410.  
  411. print " -> ";
  412. print "(".$response->code.") :";
  413. print $response->content;
  414. print "\n";
  415.  
  416.  
  417. #print "DEBUG: mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
  418.  
  419. if ($response->code == HTTP::Status::HTTP_REQUEST_TIMEOUT) {
  420. print "Connection timeout. Upload failed \n";
  421. }elsif ($response->code == 500 ) {
  422. print "Bad Hostname, upload failed. Please check your internet connection. \n";
  423. }else{
  424. #print "upload successful";
  425. #print "mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
  426. system "mv /var/spool/lhg-tools/$fname /var/spool/lhg-tools/done_$fname";
  427. }
  428. #exit 0;
  429.  
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement