Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. """Written for Python 3.5, works on 2.7.3 +
  2.  
  3. Pip install the following moduels:
  4. psycopg2==2.7.1
  5. PyYAML==3.12
  6.  
  7. Copyright (C) 2017 William Christensen
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation version 2.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  
  22. """
  23.  
  24. import psycopg2
  25. import yaml
  26. from collections import defaultdict
  27.  
  28. ## Constants
  29. DATABASE_CONFIG = '/etc/foreman/database.yml' # If using Satellite, change foreman to satellite.
  30. OUTPUTFILE_NAME = "inventory.txt"
  31.  
  32. def main():
  33. """Reads the database to pull clients that have checked into puppet within the last day.
  34. Returns a CSV to be parsed of the results."""
  35.  
  36. database = ''
  37. username = ''
  38. password = ''
  39.  
  40. try:
  41. dbfile = open(DATABASE_CONFIG)
  42. dataMap = yaml.safe_load(dbfile)
  43.  
  44. database = dataMap['production']['database']
  45. username = dataMap['production']['username']
  46. password = dataMap['production']['password']
  47.  
  48. except:
  49. print("Failed reading the database config file for The Foreman. Please edit the script for the proper name and location.")
  50. return
  51.  
  52. connection = None
  53. cur = None
  54. hosts = None
  55.  
  56. try:
  57. connection = psycopg2.connect("host='localhost' dbname='"+database+"' user='"+username+"' password='"+password+"'") # Connection to database
  58. cur = connection.cursor() # Cursor object to the database.
  59. except:
  60. print("Connection with database failed.")
  61. return
  62.  
  63. try:
  64. # Select all hosts that have reported in the last day.
  65. cur.execute("SELECT name from hosts WHERE last_report > (current_date - 1) ORDER BY name;")
  66. hosts = cur.fetchall()
  67. except:
  68. print("Could not perform select statement")
  69. return
  70.  
  71. try:
  72.  
  73. groups = defaultdict(list)
  74.  
  75. for host in hosts:
  76. endLoc = host[0].index(".")
  77. # In the event systems wish to be classified by hostname, add the logic here.
  78. ### <Change for your naming scheme here for grouping of systems> ###
  79. if(endLoc > 0 or endLoc != None):
  80. print(host[0][0:endLoc])
  81. groups[host[0][0:3]].append(host[0][0:endLoc] + "\n")
  82. else:
  83. print(host[0])
  84. groups[host[0][0:3]].append(host[0] + "\n")
  85. ### </Change for your naming scheme here for grouping of systems> ###
  86.  
  87. output = open(OUTPUTFILE_NAME,"w")
  88.  
  89. for key in groups.keys():
  90. output.write("["+ key + "]" + "\n") # print the group name by first 3 letters
  91. for host in groups[key]:
  92. output.write(host) # print the host names from the list.
  93. output.write("\n")
  94.  
  95. output.close()
  96.  
  97. except:
  98. print("hosts could not be populated or found.")
  99. return
  100.  
  101.  
  102. if __name__ == '__main__':
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement