Advertisement
dotgoat

DhcpLeaseParser

Mar 1st, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Storage;
  6. use Carbon\Carbon;
  7. use GuzzleHttp\Client;
  8.  
  9. class DhcpLeaseParser
  10. {
  11.  
  12. /**
  13. * Create a new instance.
  14. *
  15. * @return void
  16. */
  17. public function __construct()
  18. {
  19. $this->leases_file = 'dhcp/dhcpd.leases';
  20. }
  21.  
  22. /**
  23. * Get the entire contents of the leases file. Remove lines beginning with "#" before returning
  24. * The "1" flag in preg_grep inverses the matching.
  25. */
  26. private function getLeasesFile()
  27. {
  28. if (Storage::exists($this->leases_file))
  29. {
  30. $myFile = Storage::get($this->leases_file);
  31. $lines = explode("\n", $myFile);
  32. $fileWithoutComments = implode("\n", preg_grep("/^#/", $lines, 1));
  33. return $fileWithoutComments;
  34. }
  35. return 'false';
  36. }
  37.  
  38. /**
  39. * Get leases as a single dimensional array
  40. *
  41. * @return array
  42. */
  43. private function getLeasesAsSingleDimensionalArray()
  44. {
  45. $leasesArray = explode("lease", $this->getLeasesFile());
  46. $leasesArray = array_slice($leasesArray, 3);
  47. return $leasesArray;
  48. }
  49.  
  50. /**
  51. * Get leases as a multidimensional array
  52. */
  53. private function getLeasesAsMultiDimensionalArray()
  54. {
  55. $leases = [];
  56. foreach ($this->getLeasesAsSingleDimensionalArray() as $lease)
  57. {
  58. $singleLease = explode("\n", $lease);
  59. array_push($leases, $singleLease);
  60. }
  61. return $leases;
  62. }
  63.  
  64. /**
  65. * preg_grep returns an array so instead of starts and ends, etc having a string value, they
  66. * end up having an array as the value. For that reason, we implode it to make a string
  67. * out of that array
  68. */
  69.  
  70. /**
  71. * Find the IP address in the lease and then clean it up. Remember, you can just return $ip_line
  72. * here if you want to see it raw
  73. */
  74. private function getCleanIp($lease)
  75. {
  76. $ip_line = implode('', preg_grep("/^\s*[0-9]/", $lease));
  77. $ipLineParts = explode(" ", $ip_line);
  78. return $ipLineParts[1];
  79. }
  80.  
  81. /**
  82. * Get and clean the "starts" line
  83. */
  84. private function getCleanStarts($lease)
  85. {
  86. $starts_line = implode('', preg_grep("/^\s++starts/", $lease));
  87. $startsLineParts = preg_split("/\s+/", $starts_line);
  88. $date = $startsLineParts[3];
  89. $time = substr($startsLineParts[4], 0, -1);
  90.  
  91. $timestamp = ($date . ' ' . $time);
  92. $myDate = Carbon::createFromFormat('Y/m/d H:i:s', $timestamp, 'UTC');
  93. $myDate->timezone(config('app.timezone'));
  94. return $myDate;
  95. }
  96.  
  97. /**
  98. * Get and clean the "starts" line
  99. */
  100. private function getCleanEnds($lease)
  101. {
  102. $ends_line = implode('', preg_grep("/^\s++ends/", $lease));
  103. $endsLineParts = preg_split("/\s+/", $ends_line);
  104. $date = $endsLineParts[3];
  105. $time = substr($endsLineParts[4], 0, -1);
  106.  
  107. $timestamp = ($date . ' ' . $time);
  108. $myDate = Carbon::createFromFormat('Y/m/d H:i:s', $timestamp, 'UTC');
  109. $myDate->timezone(config('app.timezone'));
  110. return $myDate;
  111. }
  112.  
  113. /**
  114. * Get and clean the "binding state" line
  115. */
  116. private function getCleanBindingState($lease)
  117. {
  118. $binding_state_line = implode('', preg_grep("/^\s++binding\sstate/", $lease));
  119. $bindingStateLineParts = preg_split("/\s+/", $binding_state_line);
  120. $state = substr($bindingStateLineParts[3], 0, -1);
  121. return $state;
  122. }
  123.  
  124. /**
  125. * Get and clean the "next binding state" line
  126. */
  127. private function getCleanNextBindingState($lease)
  128. {
  129. $next_binding_state_line = implode('', preg_grep("/^\s++next\sbinding\sstate/", $lease));
  130. if ( ! empty($next_binding_state_line))
  131. {
  132. $nextBindingStateLineParts = preg_split("/\s+/", $next_binding_state_line);
  133. $state = substr($nextBindingStateLineParts[4], 0, -1);
  134. return $state;
  135. }
  136. }
  137.  
  138. /**
  139. * Get and clean the "rewind binding state" line
  140. */
  141. private function getCleanRewindBindingState($lease)
  142. {
  143. $rewind_binding_state_line = implode('', preg_grep("/^\s++rewind\sbinding\sstate/", $lease));
  144. if ( ! empty($rewind_binding_state_line))
  145. {
  146. $rewindBindingStateLineParts = preg_split("/\s+/", $rewind_binding_state_line);
  147. $state = substr($rewindBindingStateLineParts[4], 0, -1);
  148. return $state;
  149. }
  150. }
  151.  
  152. /**
  153. * Get and clean the "hardware ethernet" line
  154. */
  155. private function getCleanHardwareEthernet($lease)
  156. {
  157. $hardware_ethernet_line = implode('', preg_grep("/^\s++hardware\sethernet/", $lease));
  158. if ( ! empty($hardware_ethernet_line))
  159. {
  160. $hardwareEthernetLineParts = preg_split("/\s+/", $hardware_ethernet_line);
  161. $hardware_ethernet = substr($hardwareEthernetLineParts[3], 0, -1);
  162. return $hardware_ethernet;
  163. }
  164. }
  165.  
  166. /**
  167. * Get and clean the "option agent.circuit-id" line
  168. */
  169. private function getCleanUid($lease)
  170. {
  171. $uid_line = implode('', preg_grep("/^\s++uid/", $lease));
  172. if ( ! empty($uid_line))
  173. {
  174. $uidLineParts = preg_split("/\s+/", $uid_line);
  175. $uid = substr($uidLineParts[2], 0, -1);
  176. return $uid;
  177. }
  178. }
  179.  
  180. /**
  181. * Get and clean the "option agent.circuit-id" line
  182. */
  183. private function getCleanCircuitId($lease)
  184. {
  185. $circuit_id_line = implode('', preg_grep("/^\s++option\sagent\.circuit-id/", $lease));
  186. if ( ! empty($circuit_id_line))
  187. {
  188. $circuitIdLineParts = preg_split("/\s+/", $circuit_id_line);
  189. $circuit_id = substr($circuitIdLineParts[3], 0, -1);
  190. return $circuit_id;
  191. }
  192. }
  193.  
  194. /**
  195. * Get and clean the "option agent.remote-id" line
  196. */
  197. private function getCleanRemoteId($lease)
  198. {
  199. $remote_id_line = implode('', preg_grep("/^\s++option\sagent\.remote-id/", $lease));
  200. if ( ! empty($remote_id_line))
  201. {
  202. $remoteIdLineParts = preg_split("/\s+/", $remote_id_line);
  203. $remote_id = substr($remoteIdLineParts[3], 0, -1);
  204. return $remote_id;
  205. }
  206. }
  207.  
  208. /**
  209. * Get and clean the "option agent.subscriber-id" line
  210. */
  211. private function getCleanSubscriberId($lease)
  212. {
  213. $subscriber_id_line = implode('', preg_grep("/^\s++option\sagent\.subscriber-id/", $lease));
  214. if ( ! empty($subscriber_id_line))
  215. {
  216. $subscriberIdLineParts = preg_split("/\s+/", $subscriber_id_line);
  217. $subscriber_id = substr($subscriberIdLineParts[3], 0, -1);
  218. return $subscriber_id;
  219. }
  220. }
  221.  
  222. /**
  223. * Get all the stats provided by dhcpd-pools
  224. */
  225. public function getDhcpStats()
  226. {
  227. $client = new Client();
  228. $dhcpServer = env('DHCP_SERVER', 'localhost');
  229. $response = $client->get('http://' . $dhcpServer . '/dhcp.php');
  230. $liveDhcpInfo = $response->json();
  231. return collect($liveDhcpInfo);
  232. }
  233.  
  234. /**
  235. * Get the hash of all leases in the file
  236. */
  237. public function getAllLeases()
  238. {
  239. $leases = [];
  240. foreach ($this->getLeasesAsMultiDimensionalArray() as $lease)
  241. {
  242. $hash['ip'] = $this->getCleanIp($lease);
  243. $hash['starts'] = $this->getCleanStarts($lease);
  244. $hash['ends'] = $this->getCleanEnds($lease);
  245. // $hash['tstp'] = implode('', preg_grep("/^\s++tstp/", $lease));
  246. // $hash['cltt'] = implode('', preg_grep("/^\s++cltt/", $lease));
  247. $hash['binding_state'] = $this->getCleanBindingState($lease);
  248. $hash['next_binding_state'] = $this->getCleanNextBindingState($lease);
  249. $hash['rewind_binding_state'] = $this->getCleanRewindBindingState($lease);
  250. $hash['hardware_ethernet'] = $this->getCleanHardwareEthernet($lease);
  251. $hash['uid'] = $this->getCleanUid($lease);
  252. $hash['option_agent_circuit-id'] = $this->getCleanCircuitId($lease);
  253. $hash['option_agent_remote-id'] = $this->getCleanRemoteId($lease);
  254. $hash['option_agent_subscriber-id'] = $this->getCleanSubscriberId($lease);
  255. array_push($leases, $hash);
  256. }
  257. return collect($leases);
  258. }
  259.  
  260. /**
  261. * Get the latest entry in the file for a particular IP
  262. */
  263. public function getActiveLeases()
  264. {
  265. $leases = $this->getAllLeases()->reverse()->unique('ip');
  266. return $leases;
  267. }
  268.  
  269. /**
  270. * Get the stats for each defined shared-network
  271. */
  272. public function getDhcpSharedNetworkStats()
  273. {
  274. $stats = $this->getDhcpStats();
  275. return $stats['shared-networks'];
  276. }
  277.  
  278. /**
  279. * Get the stats for each defined subnet
  280. */
  281. public function getDhcpSubnetStats()
  282. {
  283. $stats = $this->getDhcpStats();
  284. return $stats['subnets'];
  285. }
  286.  
  287. /**
  288. * Get the summary stats for the entire dhcp server
  289. */
  290. public function getDhcpSummaryStats()
  291. {
  292. $stats = $this->getDhcpStats();
  293. return $stats['summary'];
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement