You know how many servers are federated with your Pleroma server but you don't know much about them. What software are they running? How many users? Are they still alive? These Python scripts will help you to get all that information and much more! https://libretux.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
9.2 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import getpass
  4. import os
  5. import sys
  6. from mastodon import Mastodon
  7. from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError
  8. import psycopg2
  9. from psycopg2 import sql
  10. from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
  11. # Returns the parameter from the specified file
  12. def get_parameter( parameter, file_path ):
  13. # Check if secrets file exists
  14. if not os.path.isfile(file_path):
  15. print("File %s not found, asking."%file_path)
  16. write_parameter( parameter, file_path )
  17. #sys.exit(0)
  18. # Find parameter in file
  19. with open( file_path ) as f:
  20. for line in f:
  21. if line.startswith( parameter ):
  22. return line.replace(parameter + ":", "").strip()
  23. # Cannot find parameter, exit
  24. print(file_path + " Missing parameter %s "%parameter)
  25. sys.exit(0)
  26. def write_parameter( parameter, file_path ):
  27. print("Setting up host parameters...")
  28. print("\n")
  29. pleroma_hostname = input("Enter Pleroma hostname: ")
  30. pleroma_db = input("Pleroma db name: ")
  31. pleroma_db_user = input("pleroma db user: ")
  32. fediverse_db = input("fediverse db name: ")
  33. fediverse_db_user = input("fediverse db user: ")
  34. multiprocessing_processes = input("Multiprocessing processes: ")
  35. with open(file_path, "w") as text_file:
  36. print("pleroma_hostname: {}".format(pleroma_hostname), file=text_file)
  37. print("pleroma_db: {}".format(pleroma_db), file=text_file)
  38. print("pleroma_db_user: {}".format(pleroma_db_user), file=text_file)
  39. print("fediverse_db: {}".format(fediverse_db), file=text_file)
  40. print("fediverse_db_user: {}".format(fediverse_db_user), file=text_file)
  41. print("multiprocessing_processes: {}".format(multiprocessing_processes), file=text_file)
  42. def create_table(db, db_user, table, sql):
  43. conn = None
  44. try:
  45. conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
  46. cur = conn.cursor()
  47. print("Creating table.. "+table)
  48. # Create the table in PostgreSQL database
  49. cur.execute(sql)
  50. conn.commit()
  51. print("Table "+table+" created!")
  52. print("\n")
  53. except (Exception, psycopg2.DatabaseError) as error:
  54. print(error)
  55. finally:
  56. if conn is not None:
  57. conn.close()
  58. #############################################################################################
  59. # Load configuration from config file
  60. config_filepath = "config.txt"
  61. pleroma_hostname = get_parameter("pleroma_hostname", config_filepath) # E.g., pleroma.site
  62. pleroma_db = get_parameter("pleroma_db", config_filepath) # E.g., pleroma_prod
  63. pleroma_db_user = get_parameter("pleroma_db_user", config_filepath) # E.g., pleroma
  64. fediverse_db = get_parameter("fediverse_db", config_filepath) # E.g., fediverse
  65. fediverse_db_user = get_parameter("fediverse_db_user", config_filepath) # E.g., pleroma
  66. ############################################################
  67. # create database
  68. ############################################################
  69. conn = None
  70. try:
  71. conn = psycopg2.connect(dbname='postgres',
  72. user=fediverse_db_user, host='',
  73. password='')
  74. conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  75. cur = conn.cursor()
  76. print("Creating database " + fediverse_db + ". Please wait...")
  77. cur.execute(sql.SQL("CREATE DATABASE {}").format(
  78. sql.Identifier(fediverse_db))
  79. )
  80. print("Database " + fediverse_db + " created!")
  81. except (Exception, psycopg2.DatabaseError) as error:
  82. print(error)
  83. finally:
  84. if conn is not None:
  85. conn.close()
  86. #############################################################################################
  87. try:
  88. conn = None
  89. conn = psycopg2.connect(database = fediverse_db, user = fediverse_db_user, password = "", host = "/var/run/postgresql", port = "5432")
  90. except (Exception, psycopg2.DatabaseError) as error:
  91. print(error)
  92. # Load configuration from config file
  93. os.remove("config.txt")
  94. print("Exiting. Run setup again with right parameters")
  95. sys.exit(0)
  96. if conn is not None:
  97. print("\n")
  98. print("Host parameters saved to config.txt!")
  99. print("\n")
  100. ############################################################
  101. # Create needed tables
  102. ############################################################
  103. print("Creating table...")
  104. ########################################
  105. db = fediverse_db
  106. db_user = fediverse_db_user
  107. table = "federation"
  108. sql = "create table "+table+" (server varchar(30) PRIMARY KEY, users int, federated_servers int, posts int, updated_at timestamptz,"
  109. sql += "software varchar(10), version varchar(40))"
  110. create_table(db, db_user, table, sql)
  111. #######################################
  112. db = fediverse_db
  113. db_user = fediverse_db_user
  114. table = "federated_servers"
  115. sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, visited int, mastodon int, others int, no_response int, ssl_error int, timeout_error int, connection_error int, total_servers int,"
  116. sql += "total_users int, pleroma int, mastodon_users int, pleroma_users int, gnusocial int, gnusocial_users int, zap int, zap_users int, plume int, plume_users int, hubzilla int, hubzilla_users int,"
  117. sql += "misskey int, misskey_users int, prismo int, prismo_users int, osada int, osada_users int, groundpolis int, gpolis_users int, ganggo int, ganggo_users int, squs int, squs_users int, peertube int,"
  118. sql += "peertube_users int, friendica int, friendica_users int, pixelfed int, pixelfed_users int"
  119. sql += "writefreely int, writefreely_users int, ravenvale int, ravenvale_users int")
  120. create_table(db, db_user, table, sql)
  121. #######################################
  122. db = fediverse_db
  123. db_user = fediverse_db_user
  124. table = "evo"
  125. sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, visited int, mastodon int, others int, no_response int, ssl_error int, timeout_error int, connection_error int, total_servers int,"
  126. sql += "total_users int, pleroma int, mastodon_users int, pleroma_users int, gnusocial int, gnusocial_users int, zap int, zap_users int, plume int, plume_users int, hubzilla int, hubzilla_users int,"
  127. sql += "misskey int, misskey_users int, prismo int, prismo_users int, osada int, osada_users int, groundpolis int, gpolis_users int, ganggo int, ganggo_users int, squs int, squs_users int, peertube int,"
  128. sql += "peertube_users int, friendica int, friendica_users int, pixelfed int, pixelfed_users int)"
  129. create_table(db, db_user, table, sql)
  130. #######################################
  131. db = fediverse_db
  132. db_user = fediverse_db_user
  133. table = "sslerror_servers"
  134. sql = "create table "+table+" (server varchar(30) PRIMARY KEY, added_at timestamptz, updated_at timestamptz, days varchar(30))"
  135. create_table(db, db_user, table, sql)
  136. #######################################
  137. db = fediverse_db
  138. db_user = fediverse_db_user
  139. table = "connerror_servers"
  140. sql = "create table "+table+" (server varchar(30) PRIMARY KEY, added_at timestamptz, updated_at timestamptz, days varchar(30))"
  141. create_table(db, db_user, table, sql)
  142. #######################################
  143. db = fediverse_db
  144. db_user = fediverse_db_user
  145. table = "timeouterror_servers"
  146. sql = "create table "+table+" (server varchar(30) PRIMARY KEY, added_at timestamptz, updated_at timestamptz, days varchar(30))"
  147. create_table(db, db_user, table, sql)
  148. #######################################
  149. db = fediverse_db
  150. db_user = fediverse_db_user
  151. table = "noresponse_servers"
  152. sql = "create table "+table+" (server varchar(30) PRIMARY KEY, added_at timestamptz, updated_at timestamptz, days varchar(30))"
  153. create_table(db, db_user, table, sql)
  154. #######################################
  155. db = fediverse_db
  156. db_user = fediverse_db_user
  157. table = "others_servers"
  158. sql = "create table "+table+" (server varchar(30) PRIMARY KEY, added_at timestamptz, updated_at timestamptz, days varchar(30))"
  159. create_table(db, db_user, table, sql)
  160. #######################################################
  161. # add column 'version' to federation table if not exist
  162. conn = None
  163. try:
  164. conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
  165. cur = conn.cursor()
  166. cur.execute('alter table federation add column if not exists version varchar(40)')
  167. conn.commit()
  168. except (Exception, psycopg2.DatabaseError) as error:
  169. print(error)
  170. finally:
  171. if conn is not None:
  172. conn.close()
  173. ###########################################################################################################################
  174. # add columns 'writefreely', 'writefreely_users', 'ravenvale' and 'ravenvale_users' to federated_servers table if not exist
  175. conn = None
  176. try:
  177. conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
  178. cur = conn.cursor()
  179. cur.execute('alter table federated_servers add column if not exists writefreely int')
  180. cur.execute('alter table federated_servers add column if not exists writefreely_users int')
  181. cur.execute('alter table federated_servers add column if not exists ravenvale int')
  182. cur.execute('alter table federated_servers add column if not exists ravenvale_users int')
  183. conn.commit()
  184. except (Exception, psycopg2.DatabaseError) as error:
  185. print(error)
  186. finally:
  187. if conn is not None:
  188. conn.close()
  189. #####################################
  190. print("Done!")
  191. print("Now you can run setup.py!")
  192. print("\n")