|
| 1 | +/* system statistics extension */ |
| 2 | + |
| 3 | +-- complain if script is sourced in psql, rather than via CREATE EXTENSION |
| 4 | +\echo Use "CREATE EXTENSION system_stats" to load this file. \quit |
| 5 | + |
| 6 | +-- role to be assigned while executing functions of system stats |
| 7 | +-- before creating role, check the role exists or not. It may possible |
| 8 | +-- that user want to create extension in multiple database of same server |
| 9 | +DO $$ |
| 10 | +BEGIN |
| 11 | + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'monitor_system_stats') THEN |
| 12 | + CREATE ROLE monitor_system_stats WITH |
| 13 | + NOLOGIN |
| 14 | + NOSUPERUSER |
| 15 | + NOCREATEDB |
| 16 | + NOCREATEROLE |
| 17 | + INHERIT |
| 18 | + NOREPLICATION |
| 19 | + CONNECTION LIMIT -1; |
| 20 | + END IF; |
| 21 | +END |
| 22 | +$$; |
| 23 | + |
| 24 | +-- Operating system information function |
| 25 | +CREATE FUNCTION pg_sys_os_info( |
| 26 | + OUT name text, |
| 27 | + OUT version text, |
| 28 | + OUT host_name text, |
| 29 | + OUT domain_name text, |
| 30 | + OUT handle_count int, |
| 31 | + OUT process_count int, |
| 32 | + OUT thread_count int, |
| 33 | + OUT architecture text, |
| 34 | + OUT last_bootup_time text, |
| 35 | + OUT os_up_since_seconds int |
| 36 | +) |
| 37 | +RETURNS SETOF record |
| 38 | +AS 'MODULE_PATHNAME' |
| 39 | +LANGUAGE C; |
| 40 | + |
| 41 | +REVOKE ALL ON FUNCTION pg_sys_os_info() FROM PUBLIC; |
| 42 | +GRANT EXECUTE ON FUNCTION pg_sys_os_info() TO monitor_system_stats; |
| 43 | + |
| 44 | +-- System CPU information function |
| 45 | +CREATE FUNCTION pg_sys_cpu_info( |
| 46 | + OUT vendor text, |
| 47 | + OUT description text, |
| 48 | + OUT model_name text, |
| 49 | + OUT processor_type int, |
| 50 | + OUT logical_processor int, |
| 51 | + OUT physical_processor int, |
| 52 | + OUT no_of_cores int, |
| 53 | + OUT architecture text, |
| 54 | + OUT clock_speed_hz int8, |
| 55 | + OUT cpu_type text, |
| 56 | + OUT cpu_family text, |
| 57 | + OUT byte_order text, |
| 58 | + OUT l1dcache_size int, |
| 59 | + OUT l1icache_size int, |
| 60 | + OUT l2cache_size int, |
| 61 | + OUT l3cache_size int |
| 62 | +) |
| 63 | +RETURNS SETOF record |
| 64 | +AS 'MODULE_PATHNAME' |
| 65 | +LANGUAGE C; |
| 66 | + |
| 67 | +REVOKE ALL ON FUNCTION pg_sys_cpu_info() FROM PUBLIC; |
| 68 | +GRANT EXECUTE ON FUNCTION pg_sys_cpu_info() TO monitor_system_stats; |
| 69 | + |
| 70 | +-- Memory information function |
| 71 | +CREATE FUNCTION pg_sys_memory_info( |
| 72 | + OUT total_memory int8, |
| 73 | + OUT used_memory int8, |
| 74 | + OUT free_memory int8, |
| 75 | + OUT swap_total int8, |
| 76 | + OUT swap_used int8, |
| 77 | + OUT swap_free int8, |
| 78 | + OUT cache_total int8, |
| 79 | + OUT kernel_total int8, |
| 80 | + OUT kernel_paged int8, |
| 81 | + OUT kernel_non_paged int8, |
| 82 | + OUT total_page_file int8, |
| 83 | + OUT avail_page_file int8 |
| 84 | +) |
| 85 | +RETURNS SETOF record |
| 86 | +AS 'MODULE_PATHNAME' |
| 87 | +LANGUAGE C; |
| 88 | + |
| 89 | +REVOKE ALL ON FUNCTION pg_sys_memory_info() FROM PUBLIC; |
| 90 | +GRANT EXECUTE ON FUNCTION pg_sys_memory_info() TO monitor_system_stats; |
| 91 | + |
| 92 | +-- Load average information function |
| 93 | +CREATE FUNCTION pg_sys_load_avg_info( |
| 94 | + OUT load_avg_one_minute float4, |
| 95 | + OUT load_avg_five_minutes float4, |
| 96 | + OUT load_avg_ten_minutes float4, |
| 97 | + OUT load_avg_fifteen_minutes float4 |
| 98 | +) |
| 99 | +RETURNS SETOF record |
| 100 | +AS 'MODULE_PATHNAME' |
| 101 | +LANGUAGE C; |
| 102 | + |
| 103 | +REVOKE ALL ON FUNCTION pg_sys_load_avg_info() FROM PUBLIC; |
| 104 | +GRANT EXECUTE ON FUNCTION pg_sys_load_avg_info() TO monitor_system_stats; |
| 105 | + |
| 106 | +-- network information function |
| 107 | +CREATE FUNCTION pg_sys_network_info( |
| 108 | + OUT interface_name text, |
| 109 | + OUT ip_address text, |
| 110 | + OUT tx_bytes int8, |
| 111 | + OUT tx_packets int8, |
| 112 | + OUT tx_errors int8, |
| 113 | + OUT tx_dropped int8, |
| 114 | + OUT rx_bytes int8, |
| 115 | + OUT rx_packets int8, |
| 116 | + OUT rx_errors int8, |
| 117 | + OUT rx_dropped int8, |
| 118 | + OUT link_speed_mbps int |
| 119 | +) |
| 120 | +RETURNS SETOF record |
| 121 | +AS 'MODULE_PATHNAME' |
| 122 | +LANGUAGE C; |
| 123 | + |
| 124 | +REVOKE ALL ON FUNCTION pg_sys_network_info() FROM PUBLIC; |
| 125 | +GRANT EXECUTE ON FUNCTION pg_sys_network_info() TO monitor_system_stats; |
| 126 | + |
| 127 | +-- CPU and memory information by process id or name |
| 128 | +CREATE FUNCTION pg_sys_cpu_memory_by_process( |
| 129 | + OUT pid int, |
| 130 | + OUT name text, |
| 131 | + OUT running_since_seconds int8, |
| 132 | + OUT cpu_usage float4, |
| 133 | + OUT memory_usage float4, |
| 134 | + OUT memory_bytes int8 |
| 135 | +) |
| 136 | +RETURNS SETOF record |
| 137 | +AS 'MODULE_PATHNAME' |
| 138 | +LANGUAGE C; |
| 139 | + |
| 140 | +REVOKE ALL ON FUNCTION pg_sys_cpu_memory_by_process() FROM PUBLIC; |
| 141 | +GRANT EXECUTE ON FUNCTION pg_sys_cpu_memory_by_process() TO monitor_system_stats; |
| 142 | + |
| 143 | +-- Disk information function |
| 144 | +CREATE FUNCTION pg_sys_disk_info( |
| 145 | + OUT mount_point text, |
| 146 | + OUT file_system text, |
| 147 | + OUT drive_letter text, |
| 148 | + OUT drive_type int, |
| 149 | + OUT file_system_type text, |
| 150 | + OUT total_space int8, |
| 151 | + OUT used_space int8, |
| 152 | + OUT free_space int8, |
| 153 | + OUT total_inodes int8, |
| 154 | + OUT used_inodes int8, |
| 155 | + OUT free_inodes int8 |
| 156 | +) |
| 157 | +RETURNS SETOF record |
| 158 | +AS 'MODULE_PATHNAME' |
| 159 | +LANGUAGE C; |
| 160 | + |
| 161 | +REVOKE ALL ON FUNCTION pg_sys_disk_info() FROM PUBLIC; |
| 162 | +GRANT EXECUTE ON FUNCTION pg_sys_disk_info() TO monitor_system_stats; |
| 163 | + |
| 164 | +-- process information function |
| 165 | +CREATE FUNCTION pg_sys_process_info( |
| 166 | + OUT total_processes int, |
| 167 | + OUT running_processes int, |
| 168 | + OUT sleeping_processes int, |
| 169 | + OUT stopped_processes int, |
| 170 | + OUT zombie_processes int |
| 171 | +) |
| 172 | +RETURNS SETOF record |
| 173 | +AS 'MODULE_PATHNAME' |
| 174 | +LANGUAGE C; |
| 175 | + |
| 176 | +REVOKE ALL ON FUNCTION pg_sys_process_info() FROM PUBLIC; |
| 177 | +GRANT EXECUTE ON FUNCTION pg_sys_process_info() TO monitor_system_stats; |
| 178 | + |
| 179 | +-- CPU usage information function |
| 180 | +-- This function will fetch the time spent in percentage by CPU in each mode |
| 181 | +-- as described by arguments |
| 182 | +CREATE FUNCTION pg_sys_cpu_usage_info( |
| 183 | + OUT usermode_normal_process_percent float4, |
| 184 | + OUT usermode_niced_process_percent float4, |
| 185 | + OUT kernelmode_process_percent float4, |
| 186 | + OUT idle_mode_percent float4, |
| 187 | + OUT IO_completion_percent float4, |
| 188 | + OUT servicing_irq_percent float4, |
| 189 | + OUT servicing_softirq_percent float4, |
| 190 | + OUT user_time_percent float4, |
| 191 | + OUT processor_time_percent float4, |
| 192 | + OUT privileged_time_percent float4, |
| 193 | + OUT interrupt_time_percent float4 |
| 194 | +) |
| 195 | +RETURNS SETOF record |
| 196 | +AS 'MODULE_PATHNAME' |
| 197 | +LANGUAGE C; |
| 198 | + |
| 199 | +REVOKE ALL ON FUNCTION pg_sys_cpu_usage_info() FROM PUBLIC; |
| 200 | +GRANT EXECUTE ON FUNCTION pg_sys_cpu_usage_info() TO monitor_system_stats; |
| 201 | + |
| 202 | +-- IO analysis information function |
| 203 | +CREATE FUNCTION pg_sys_io_analysis_info( |
| 204 | + OUT device_name text, |
| 205 | + OUT total_reads int8, |
| 206 | + OUT total_writes int8, |
| 207 | + OUT read_bytes int8, |
| 208 | + OUT write_bytes int8, |
| 209 | + OUT read_time_ms int8, |
| 210 | + OUT write_time_ms int8 |
| 211 | +) |
| 212 | +RETURNS SETOF record |
| 213 | +AS 'MODULE_PATHNAME' |
| 214 | +LANGUAGE C; |
| 215 | + |
| 216 | +REVOKE ALL ON FUNCTION pg_sys_io_analysis_info() FROM PUBLIC; |
| 217 | +GRANT EXECUTE ON FUNCTION pg_sys_io_analysis_info() TO monitor_system_stats; |
| 218 | + |
| 219 | + |
0 commit comments