Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. #!/bin/sh
  2. # "-----------------------------------------------------------"
  3. # " This is a simple script for creating a query "
  4. # " that collects data from the table pg_stat_user_tables "
  5. # " for list of databases "
  6. # "-----------------------------------------------------------"
  7.  
  8. if [ "$#" -eq 0 ] ;
  9. then
  10. echo -e "\n\tYou must enter at least one database name as an argument."
  11. echo -e "\tArguments should be separated by spaces.\n"
  12. exit
  13. fi
  14.  
  15. TOPPART="\npg_stat_user_tables:\n query: \"SELECT datname, "
  16. MIDDLEPART="FROM pg_database"
  17. BOTTOMPART="WHERE datname NOT LIKE 'template_' AND datname NOT LIKE 'postgres'\""
  18. PROMPART=" metrics:
  19. - datname:
  20. usage: \"LABEL\"
  21. description: \"Name of the database that this table is in\"
  22. - schemaname:
  23. usage: \"LABEL\"
  24. description: \"Name of the schema that this table is in\"
  25. - relname:
  26. usage: \"LABEL\"
  27. description: \"Name of this table\"
  28. - seq_scan:
  29. usage: \"COUNTER\"
  30. description: \"Number of sequential scans initiated on this table\"
  31. - seq_tup_read:
  32. usage: \"COUNTER\"
  33. description: \"Number of live rows fetched by sequential scans\"
  34. - idx_scan:
  35. usage: \"COUNTER\"
  36. description: \"Number of index scans initiated on this table\"
  37. - idx_tup_fetch:
  38. usage: \"COUNTER\"
  39. description: \"Number of live rows fetched by index scans\"
  40. - n_tup_ins:
  41. usage: \"COUNTER\"
  42. description: \"Number of rows inserted\"
  43. - n_tup_upd:
  44. usage: \"COUNTER\"
  45. description: \"Number of rows updated\"
  46. - n_tup_del:
  47. usage: \"COUNTER\"
  48. description: \"Number of rows deleted\"
  49. - n_tup_hot_upd:
  50. usage: \"COUNTER\"
  51. description: \"Number of rows HOT updated (i.e., with no separate index update required)\"
  52. - n_live_tup:
  53. usage: \"GAUGE\"
  54. description: \"Estimated number of live rows\"
  55. - n_dead_tup:
  56. usage: \"GAUGE\"
  57. description: \"Estimated number of dead rows\"
  58. - n_mod_since_analyze:
  59. usage: \"GAUGE\"
  60. description: \"Estimated number of rows changed since last analyze\"
  61. - last_vacuum:
  62. usage: \"GAUGE\"
  63. description: \"Last time at which this table was manually vacuumed (not counting VACUUM FULL)\"
  64. - last_autovacuum:
  65. usage: \"GAUGE\"
  66. description: \"Last time at which this table was vacuumed by the autovacuum daemon\"
  67. - last_analyze:
  68. usage: \"GAUGE\"
  69. description: \"Last time at which this table was manually analyzed\"
  70. - last_autoanalyze:
  71. usage: \"GAUGE\"
  72. description: \"Last time at which this table was analyzed by the autovacuum daemon\"
  73. - vacuum_count:
  74. usage: \"COUNTER\"
  75. description: \"Number of times this table has been manually vacuumed (not counting VACUUM FULL)\"
  76. - autovacuum_count:
  77. usage: \"COUNTER\"
  78. description: \"Number of times this table has been vacuumed by the autovacuum daemon\"
  79. - analyze_count:
  80. usage: \"COUNTER\"
  81. description: \"Number of times this table has been manually analyzed\"
  82. - autoanalyze_count:
  83. usage: \"COUNTER\"
  84. description: \"Number of times this table has been analyzed by the autovacuum daemon\"
  85. "
  86.  
  87. while [ "$1" != "" ]; do
  88. OBJECTS=$OBJECTS" "$1
  89. SCHEMANAME=$SCHEMANAME$1".schemaname"
  90. RELNAME=$RELNAME$1".relname"
  91. SEQSCAN=$SEQSCAN$1".seq_scan"
  92. SEQTUPREAD=$SEQTUPREAD$1".seq_tup_read"
  93. IDXSCAN=$IDXSCAN$1".idx_scan"
  94. IDXTUPFETCH=$IDXTUPFETCH$1".idx_tup_fetch"
  95. NTUPINS=$NTUPINS$1".n_tup_ins"
  96. NTUPUPD=$NTUPUPD$1".n_tup_upd"
  97. NTUPDEL=$NTUPDEL$1".n_tup_del"
  98. NTUPHOTUPD=$NTUPHOTUPD$1".n_tup_hot_upd"
  99. NLIVETUP=$NLIVETUP$1".n_live_tup"
  100. NDEADTUP=$NDEADTUP$1".n_dead_tup"
  101. NMODSINCEANALYZE=$NMODSINCEANALYZE$1".n_mod_since_analyze"
  102. LASTVACUUM=$LASTVACUUM$1".last_vacuum"
  103. LASTAUTOVACUUM=$LASTAUTOVACUUM$1".last_autovacuum"
  104. LASTANALYZE=$LASTANALYZE$1".last_analyze"
  105. LASTAUTOANALYZE=$LASTAUTOANALYZE$1".last_autoanalyze"
  106. VACUUMCOUNT=$VACUUMCOUNT$1".vacuum_count"
  107. AUTOVACUUMCOUNT=$AUTOVACUUMCOUNT$1".autovacuum_count"
  108. ANALYZECOUNT=$ANALYZECOUNT$1".analyze_count"
  109. AUTOANALYZECOUNT=$AUTOANALYZECOUNT$1".autoanalyze_count"
  110. if [ $# -ne 1 ] ;
  111. then
  112. SCHEMANAME=$SCHEMANAME", "
  113. RELNAME=$RELNAME", "
  114. SEQSCAN=$SEQSCAN", "
  115. SEQTUPREAD=$SEQTUPREAD", "
  116. IDXSCAN=$IDXSCAN", "
  117. IDXTUPFETCH=$IDXTUPFETCH", "
  118. NTUPINS=$NTUPINS", "
  119. NTUPUPD=$NTUPUPD", "
  120. NTUPDEL=$NTUPDEL", "
  121. NTUPHOTUPD=$NTUPHOTUPD", "
  122. NLIVETUP=$NLIVETUP", "
  123. NDEADTUP=$NDEADTUP", "
  124. NMODSINCEANALYZE=$NMODSINCEANALYZE", "
  125. LASTVACUUM=$LASTVACUUM", "
  126. LASTAUTOVACUUM=$LASTAUTOVACUUM", "
  127. LASTANALYZE=$LASTANALYZE", "
  128. LASTAUTOANALYZE=$LASTAUTOANALYZE", "
  129. VACUUMCOUNT=$VACUUMCOUNT", "
  130. AUTOVACUUMCOUNT=$AUTOVACUUMCOUNT", "
  131. ANALYZECOUNT=$ANALYZECOUNT", "
  132. AUTOANALYZECOUNT=$AUTOANALYZECOUNT", "
  133. else
  134. SCHEMANAME="COALESCE("$SCHEMANAME") as ${SCHEMANAME//*./}"
  135. RELNAME="COALESCE("$RELNAME") as ${RELNAME//*./}"
  136. SEQSCAN="COALESCE("$SEQSCAN") as ${SEQSCAN//*./}"
  137. SEQTUPREAD="COALESCE("$SEQTUPREAD") as ${SEQTUPREAD//*./}"
  138. IDXSCAN="COALESCE("$IDXSCAN") as ${IDXSCAN//*./}"
  139. IDXTUPFETCH="COALESCE("$IDXTUPFETCH") as ${IDXTUPFETCH//*./}"
  140. NTUPINS="COALESCE("$NTUPINS") as ${NTUPINS//*./}"
  141. NTUPUPD="COALESCE("$NTUPUPD") as ${NTUPUPD//*./}"
  142. NTUPDEL="COALESCE("$NTUPDEL") as ${NTUPDEL//*./}"
  143. NTUPHOTUPD="COALESCE("$NTUPHOTUPD") as ${NTUPHOTUPD//*./}"
  144. NLIVETUP="COALESCE("$NLIVETUP") as ${NLIVETUP//*./}"
  145. NDEADTUP="COALESCE("$NDEADTUP") as ${NDEADTUP//*./}"
  146. NMODSINCEANALYZE="COALESCE("$NMODSINCEANALYZE") as ${NMODSINCEANALYZE//*./}"
  147. LASTVACUUM="COALESCE("$LASTVACUUM") as ${LASTVACUUM//*./}"
  148. LASTAUTOVACUUM="COALESCE("$LASTAUTOVACUUM") as ${LASTAUTOVACUUM//*./}"
  149. LASTANALYZE="COALESCE("$LASTANALYZE") as ${LASTANALYZE//*./}"
  150. LASTAUTOANALYZE="COALESCE("$LASTAUTOANALYZE") as ${LASTAUTOANALYZE//*./}"
  151. VACUUMCOUNT="COALESCE("$VACUUMCOUNT") as ${VACUUMCOUNT//*./}"
  152. AUTOVACUUMCOUNT="COALESCE("$AUTOVACUUMCOUNT") as ${AUTOVACUUMCOUNT//*./}"
  153. ANALYZECOUNT="COALESCE("$ANALYZECOUNT") as ${ANALYZECOUNT//*./}"
  154. AUTOANALYZECOUNT="COALESCE("$AUTOANALYZECOUNT") as ${AUTOANALYZECOUNT//*./}"
  155. fi
  156. shift
  157. done
  158.  
  159. echo -ne "$TOPPART"
  160. echo -n "$SCHEMANAME, $RELNAME, $SEQSCAN, $SEQTUPREAD, $IDXSCAN, $IDXTUPFETCH, $NTUPINS, $NTUPUPD, $NTUPDEL, $NTUPHOTUPD, $NLIVETUP, $NDEADTUP, $NMODSINCEANALYZE, "
  161. echo -n "$LASTVACUUM, $LASTAUTOVACUUM, $LASTANALYZE, $LASTAUTOANALYZE, $VACUUMCOUNT, $AUTOVACUUMCOUNT, $ANALYZECOUNT, $AUTOANALYZECOUNT "
  162. echo -n "$MIDDLEPART"
  163.  
  164. for t in $OBJECTS; do
  165. DB=$DB" LEFT OUTER JOIN dblink('dbname=$t','SELECT current_database(), schemaname, relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, n_live_tup, n_dead_tup, n_mod_since_analyze, last_vacuum, last_autovacuum, last_analyze, last_autoanalyze, vacuum_count, autovacuum_count, analyze_count, autoanalyze_count FROM pg_stat_user_tables') as $t(datname name, schemaname name, relname name, seq_scan bigint, seq_tup_read bigint, idx_scan bigint, idx_tup_fetch bigint, n_tup_ins bigint, n_tup_upd bigint, n_tup_del bigint, n_tup_hot_upd bigint, n_live_tup bigint, n_dead_tup bigint, n_mod_since_analyze bigint, last_vacuum timestamp, last_autovacuum timestamp, last_analyze timestamp, last_autoanalyze timestamp, vacuum_count bigint, autovacuum_count bigint, analyze_count bigint, autoanalyze_count bigint) USING (datname)"
  166. done
  167.  
  168. echo -n "$DB "
  169. echo "$BOTTOMPART"
  170. echo -e "$PROMPART"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement