interfaces.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #! /usr/bin/php
  2. <?php
  3. /*
  4. Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
  5. All rights reserved.
  6. Contact: Barry O'Donovan - barry (at) opensolutions (dot) ie
  7. http://www.opensolutions.ie/
  8. This file is part of the OSS_SNMP package.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of Open Source Solutions Limited nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. // This is an example script for OSS_SNMP
  31. //
  32. // It needs to be called with a hostname / IP address and a community string
  33. if( count( $argv ) != 3 && count( $argv ) != 4 )
  34. {
  35. echo <<< HELPTEXT
  36. OSS_SNMP - A PHP SNMP library for people who hate SNMP MIBs and OIDs!
  37. Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
  38. All rights reserved.
  39. See: https://github.com/opensolutions/OSS_SNMP/
  40. This is an example script to show how to use OSS_SNMP. It requires two or three arguments:
  41. - the IP address of hostname of a SNMP capable host
  42. - the SNMP v2 community string for that host
  43. - the index of the interface to show details for
  44. If the third argument is missing, it will print interface indexes and names.
  45. For example:
  46. {$argv[0]} 192.168.10.20 public
  47. HELPTEXT;
  48. exit( 1 );
  49. }
  50. require_once( dirname( __FILE__ ) . '/../OSS_SNMP/SNMP.php' );
  51. $host = new \OSS_SNMP\SNMP( $argv[1], $argv[2] );
  52. if( count( $argv ) == 3 )
  53. {
  54. echo "\nNumber of interfaces on {$argv[1]}: " . $host->useIface()->numberofInterfaces() . "\n\n";
  55. echo "ID: Name - Descrition - Type - Admin/Operational State\n\n";
  56. foreach( $host->useIface()->names() as $id => $name )
  57. {
  58. echo "{$id}: {$name} - {$host->useIface()->descriptions()[$id]} - {$host->useIface()->types(1)[$id]}"
  59. . " - {$host->useIface()->adminStates(1)[$id]}/{$host->useIface()->operationStates(1)[$id]}\n";
  60. }
  61. echo "\n";
  62. exit( 0 );
  63. }
  64. $names = $host->useIface()->names();
  65. $id = $argv[3];
  66. if( !isset( $names[ $id ] ) )
  67. {
  68. echo "Unknown interface index!\n";
  69. exit( 2 );
  70. }
  71. $hdr = "\nInterface information for {$names[$id]} ({$host->useIface()->descriptions()[$id]})";
  72. echo $hdr . "\n". str_repeat( '=', strlen( $hdr ) ) . "\n\n";
  73. echo <<<INTINFO
  74. Alias: {$host->useIface()->aliases()[$id]}
  75. Type: {$host->useIface()->types(1)[$id]}
  76. Admin / Operational State: {$host->useIface()->adminStates(1)[$id]}/{$host->useIface()->operationStates(1)[$id]}
  77. MTU: {$host->useIface()->mtus()[$id]}
  78. Speeds: {$host->useIface()->speeds()[$id]}
  79. Physical Address: {$host->useIface()->physAddresses()[$id]}
  80. Last Change: {$host->useIface()->lastChanges()[$id]}
  81. INTINFO;
  82. try
  83. {
  84. echo <<<INTINFO
  85. In/Out Octets: {$host->useIface()->inOctets()[$id]} / {$host->useIface()->outOctets()[$id]}
  86. In/Out Unicast: {$host->useIface()->inUnicastPackets()[$id]} / {$host->useIface()->outUnicastPackets()[$id]}
  87. In/Out Non Unicats: {$host->useIface()->inNonUnicastPackets()[$id]} / {$host->useIface()->outNonUnicastPackets()[$id]}
  88. In/Out Discards: {$host->useIface()->inDiscards()[$id]} / {$host->useIface()->outDiscards()[$id]}
  89. In/Out Errors: {$host->useIface()->inErrors()[$id]} / {$host->useIface()->outErrors()[$id]}
  90. In Unknown Protocols: {$host->useIface()->inUnknownProtocols()[$id]}
  91. Out Queue Length: {$host->useIface()->outQueueLength()[$id]}
  92. INTINFO;
  93. }
  94. catch( \OSS_SNMP\Exception $e )
  95. {
  96. echo "\nCould not poll interface statistics for this interface.\n";
  97. }
  98. exit( 0 );