Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App;
- use Storage;
- use Carbon\Carbon;
- use GuzzleHttp\Client;
- class DhcpLeaseParser
- {
- /**
- * Create a new instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->leases_file = 'dhcp/dhcpd.leases';
- }
- /**
- * Get the entire contents of the leases file. Remove lines beginning with "#" before returning
- * The "1" flag in preg_grep inverses the matching.
- */
- private function getLeasesFile()
- {
- if (Storage::exists($this->leases_file))
- {
- $myFile = Storage::get($this->leases_file);
- $lines = explode("\n", $myFile);
- $fileWithoutComments = implode("\n", preg_grep("/^#/", $lines, 1));
- return $fileWithoutComments;
- }
- return 'false';
- }
- /**
- * Get leases as a single dimensional array
- *
- * @return array
- */
- private function getLeasesAsSingleDimensionalArray()
- {
- $leasesArray = explode("lease", $this->getLeasesFile());
- $leasesArray = array_slice($leasesArray, 3);
- return $leasesArray;
- }
- /**
- * Get leases as a multidimensional array
- */
- private function getLeasesAsMultiDimensionalArray()
- {
- $leases = [];
- foreach ($this->getLeasesAsSingleDimensionalArray() as $lease)
- {
- $singleLease = explode("\n", $lease);
- array_push($leases, $singleLease);
- }
- return $leases;
- }
- /**
- * preg_grep returns an array so instead of starts and ends, etc having a string value, they
- * end up having an array as the value. For that reason, we implode it to make a string
- * out of that array
- */
- /**
- * Find the IP address in the lease and then clean it up. Remember, you can just return $ip_line
- * here if you want to see it raw
- */
- private function getCleanIp($lease)
- {
- $ip_line = implode('', preg_grep("/^\s*[0-9]/", $lease));
- $ipLineParts = explode(" ", $ip_line);
- return $ipLineParts[1];
- }
- /**
- * Get and clean the "starts" line
- */
- private function getCleanStarts($lease)
- {
- $starts_line = implode('', preg_grep("/^\s++starts/", $lease));
- $startsLineParts = preg_split("/\s+/", $starts_line);
- $date = $startsLineParts[3];
- $time = substr($startsLineParts[4], 0, -1);
- $timestamp = ($date . ' ' . $time);
- $myDate = Carbon::createFromFormat('Y/m/d H:i:s', $timestamp, 'UTC');
- $myDate->timezone(config('app.timezone'));
- return $myDate;
- }
- /**
- * Get and clean the "starts" line
- */
- private function getCleanEnds($lease)
- {
- $ends_line = implode('', preg_grep("/^\s++ends/", $lease));
- $endsLineParts = preg_split("/\s+/", $ends_line);
- $date = $endsLineParts[3];
- $time = substr($endsLineParts[4], 0, -1);
- $timestamp = ($date . ' ' . $time);
- $myDate = Carbon::createFromFormat('Y/m/d H:i:s', $timestamp, 'UTC');
- $myDate->timezone(config('app.timezone'));
- return $myDate;
- }
- /**
- * Get and clean the "binding state" line
- */
- private function getCleanBindingState($lease)
- {
- $binding_state_line = implode('', preg_grep("/^\s++binding\sstate/", $lease));
- $bindingStateLineParts = preg_split("/\s+/", $binding_state_line);
- $state = substr($bindingStateLineParts[3], 0, -1);
- return $state;
- }
- /**
- * Get and clean the "next binding state" line
- */
- private function getCleanNextBindingState($lease)
- {
- $next_binding_state_line = implode('', preg_grep("/^\s++next\sbinding\sstate/", $lease));
- if ( ! empty($next_binding_state_line))
- {
- $nextBindingStateLineParts = preg_split("/\s+/", $next_binding_state_line);
- $state = substr($nextBindingStateLineParts[4], 0, -1);
- return $state;
- }
- }
- /**
- * Get and clean the "rewind binding state" line
- */
- private function getCleanRewindBindingState($lease)
- {
- $rewind_binding_state_line = implode('', preg_grep("/^\s++rewind\sbinding\sstate/", $lease));
- if ( ! empty($rewind_binding_state_line))
- {
- $rewindBindingStateLineParts = preg_split("/\s+/", $rewind_binding_state_line);
- $state = substr($rewindBindingStateLineParts[4], 0, -1);
- return $state;
- }
- }
- /**
- * Get and clean the "hardware ethernet" line
- */
- private function getCleanHardwareEthernet($lease)
- {
- $hardware_ethernet_line = implode('', preg_grep("/^\s++hardware\sethernet/", $lease));
- if ( ! empty($hardware_ethernet_line))
- {
- $hardwareEthernetLineParts = preg_split("/\s+/", $hardware_ethernet_line);
- $hardware_ethernet = substr($hardwareEthernetLineParts[3], 0, -1);
- return $hardware_ethernet;
- }
- }
- /**
- * Get and clean the "option agent.circuit-id" line
- */
- private function getCleanUid($lease)
- {
- $uid_line = implode('', preg_grep("/^\s++uid/", $lease));
- if ( ! empty($uid_line))
- {
- $uidLineParts = preg_split("/\s+/", $uid_line);
- $uid = substr($uidLineParts[2], 0, -1);
- return $uid;
- }
- }
- /**
- * Get and clean the "option agent.circuit-id" line
- */
- private function getCleanCircuitId($lease)
- {
- $circuit_id_line = implode('', preg_grep("/^\s++option\sagent\.circuit-id/", $lease));
- if ( ! empty($circuit_id_line))
- {
- $circuitIdLineParts = preg_split("/\s+/", $circuit_id_line);
- $circuit_id = substr($circuitIdLineParts[3], 0, -1);
- return $circuit_id;
- }
- }
- /**
- * Get and clean the "option agent.remote-id" line
- */
- private function getCleanRemoteId($lease)
- {
- $remote_id_line = implode('', preg_grep("/^\s++option\sagent\.remote-id/", $lease));
- if ( ! empty($remote_id_line))
- {
- $remoteIdLineParts = preg_split("/\s+/", $remote_id_line);
- $remote_id = substr($remoteIdLineParts[3], 0, -1);
- return $remote_id;
- }
- }
- /**
- * Get and clean the "option agent.subscriber-id" line
- */
- private function getCleanSubscriberId($lease)
- {
- $subscriber_id_line = implode('', preg_grep("/^\s++option\sagent\.subscriber-id/", $lease));
- if ( ! empty($subscriber_id_line))
- {
- $subscriberIdLineParts = preg_split("/\s+/", $subscriber_id_line);
- $subscriber_id = substr($subscriberIdLineParts[3], 0, -1);
- return $subscriber_id;
- }
- }
- /**
- * Get all the stats provided by dhcpd-pools
- */
- public function getDhcpStats()
- {
- $client = new Client();
- $dhcpServer = env('DHCP_SERVER', 'localhost');
- $response = $client->get('http://' . $dhcpServer . '/dhcp.php');
- $liveDhcpInfo = $response->json();
- return collect($liveDhcpInfo);
- }
- /**
- * Get the hash of all leases in the file
- */
- public function getAllLeases()
- {
- $leases = [];
- foreach ($this->getLeasesAsMultiDimensionalArray() as $lease)
- {
- $hash['ip'] = $this->getCleanIp($lease);
- $hash['starts'] = $this->getCleanStarts($lease);
- $hash['ends'] = $this->getCleanEnds($lease);
- // $hash['tstp'] = implode('', preg_grep("/^\s++tstp/", $lease));
- // $hash['cltt'] = implode('', preg_grep("/^\s++cltt/", $lease));
- $hash['binding_state'] = $this->getCleanBindingState($lease);
- $hash['next_binding_state'] = $this->getCleanNextBindingState($lease);
- $hash['rewind_binding_state'] = $this->getCleanRewindBindingState($lease);
- $hash['hardware_ethernet'] = $this->getCleanHardwareEthernet($lease);
- $hash['uid'] = $this->getCleanUid($lease);
- $hash['option_agent_circuit-id'] = $this->getCleanCircuitId($lease);
- $hash['option_agent_remote-id'] = $this->getCleanRemoteId($lease);
- $hash['option_agent_subscriber-id'] = $this->getCleanSubscriberId($lease);
- array_push($leases, $hash);
- }
- return collect($leases);
- }
- /**
- * Get the latest entry in the file for a particular IP
- */
- public function getActiveLeases()
- {
- $leases = $this->getAllLeases()->reverse()->unique('ip');
- return $leases;
- }
- /**
- * Get the stats for each defined shared-network
- */
- public function getDhcpSharedNetworkStats()
- {
- $stats = $this->getDhcpStats();
- return $stats['shared-networks'];
- }
- /**
- * Get the stats for each defined subnet
- */
- public function getDhcpSubnetStats()
- {
- $stats = $this->getDhcpStats();
- return $stats['subnets'];
- }
- /**
- * Get the summary stats for the entire dhcp server
- */
- public function getDhcpSummaryStats()
- {
- $stats = $this->getDhcpStats();
- return $stats['summary'];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement