Tuesday, August 9, 2022
Monday, October 18, 2021
Detect slow PostgreSQL queries, pg_stat vs auto logging overhead.
Detect slow PostgreSQL queries, pg_stat vs auto logging overhead
To be able to find and detect slow
queries on a RDBMS PostgreSQL is an important thing as the database tends to
grow. Optimizing performance and expensive SQL queries is one of the major task
of the database system maintainers. These problems can be approached with many
ways and external systems, but I have tried to obtain the fast and “keep it
simple” way, which I will try to explain in this blog post.
| source: stackify |
One of the cool PostgreSQL features is
the auto_explain module , which provides a means for logging execution plans of
slow statements automatically, without having to run EXPLAIN by hand (stated by
the documentation). It is also stated in the documentation that there is a
price in overhead for that. I would suggest not using the excessive logging
from auto_explain on production systems.
The alternative that I am using
often is the pg_stat_statements module which can be easily configured on a
production or test system.
After that
we need to run CREATE EXTENSION pg_stat_statements in our databases, which will
allow PostgreSQL to create a view for us.
SELECT * FROM
pg_stat_statement
The created
view is able to tell us, which query has been executed how often and show us
insights about the total runtime of this type of query as well as about the distribution
of runtimes for those particular queries.
This allows me to get a quick overview of the CPU percentage being used and a quick overview of the I/O behavior of many types of queries, which could be a great deal of the reason that causes high loads on your production systems.
Tuesday, October 1, 2019
Oracle Blockchain contributions
Oracle Blockchain Contributions
![]() | |||
| source: Oracle Blog |
Tuesday, April 23, 2019
Server side request forgery (SSRF) attack
Server side request forgery (SSRF) attack
![]() |
| source: Acunetix |
The above code will fetch data from a URL using PHP’s file_get_contents() function and then save it to the disk. A legitime request would then look like:
Mitigation
Uncomplicated Firewall with Python
Uncomplicated Firewall with Python
| ## Description : |
| ## Generate ip-host binding list for a list of nodes, when internal DNS is missing. |
| ## 1. For existing nodes, allow traffic from new nodes |
| ## 2. For new nodes, allow traffic from all nodes |
| ## |
| ## Sample: |
| ## python ./ufw_allow_ip.py --old_ip_list_file /tmp/old_ip_list --new_ip_list_file /tmp/new_ip_list \ |
| ## --ssh_username root --ssh_port 22 --ssh_key_file ~/.ssh/id_rsa |
| ## |
| ##------------------------------------------------------------------- |
| import os, sys |
| import paramiko |
| import argparse |
| # multiple threading for a list of ssh servers |
| import Queue |
| import threading |
| import logging |
| log_folder = "%s/log" % (os.path.expanduser('~')) |
| if os.path.exists(log_folder) is False: |
| os.makedirs(log_folder) |
| log_file = "%s/%s.log" % (log_folder, os.path.basename(__file__).rstrip('\.py')) |
| logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s %(message)s') |
| logging.getLogger().addHandler(logging.StreamHandler()) |
| def get_list_from_file(fname): |
| l = [] |
| with open(fname,'r') as f: |
| for row in f: |
| row = row.strip() |
| if row.startswith('#') or row == '': |
| continue |
| l.append(row) |
| return l |
| def ufw_allow_ip_list(server_ip, ip_list, ssh_connect_args): |
| if len(ip_list) == 0: |
| print("Skip run ufw update in %s, since ip_list is empty" % (server_ip)) |
| return("OK", "") |
| [ssh_username, ssh_port, ssh_key_file, key_passphrase] = ssh_connect_args |
| ssh_command = "" |
| # TODO: improve this command, by using a library |
| for ip in ip_list: |
| ssh_command = "%s && ufw allow from %s" % (ssh_command, ip) |
| if ssh_command.startswith(" && "): |
| ssh_command = ssh_command[len(" && "):] |
| print("Update ufw in %s. ssh_command: %s" % (server_ip, ssh_command)) |
| output = "" |
| try: |
| ssh = paramiko.SSHClient() |
| ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| key = paramiko.RSAKey.from_private_key_file(ssh_key_file, password=key_passphrase) |
| ssh.connect(server_ip, username=ssh_username, port=ssh_port, pkey=key) |
| stdin, stdout, stderr = ssh.exec_command(ssh_command) |
| output = "\n".join(stdout.readlines()) |
| output = output.rstrip("\n") |
| print("Command output in %s: %s" % (server_ip, output)) |
| ssh.close() |
| except: |
| return ("ERROR", "Unexpected on server: %s error: %s\n" % (server_ip, sys.exc_info()[0])) |
| return ("OK", output) |
| ############################################################### |
| if __name__ == '__main__': |
| # get parameters from users |
| parser = argparse.ArgumentParser() |
| parser.add_argument('--old_ip_list_file', required=True, \ |
| help="IP list of current cluster", type=str) |
| parser.add_argument('--new_ip_list_file', required=True, \ |
| help="IP list of new nodes", type=str) |
| parser.add_argument('--ssh_username', required=False, default="root", \ |
| help="Which OS user to ssh", type=str) |
| parser.add_argument('--ssh_port', required=False, default="22", \ |
| help="Which port to connect sshd", type=int) |
| parser.add_argument('--ssh_key_file', required=False, default="%s/.ssh/id_rsa" % os.path.expanduser('~'), \ |
| help="ssh key file to connect", type=str) |
| parser.add_argument('--key_passphrase', required=False, default="", \ |
| help="Which OS user to ssh", type=str) |
| l = parser.parse_args() |
| ssh_connect_args = [l.ssh_username, l.ssh_port, l.ssh_key_file, l.key_passphrase] |
| old_ip_list = get_list_from_file(l.old_ip_list_file) |
| new_ip_list = get_list_from_file(l.new_ip_list_file) |
| has_error = False |
| # TODO: speed up this process by multiple threading |
| for old_ip in old_ip_list: |
| (status, output) = ufw_allow_ip_list(old_ip, new_ip_list, ssh_connect_args) |
| if status != "OK": |
| has_error = True |
| print("Error in %s. errmsg: %s" % (old_ip, output)) |
| for new_ip in new_ip_list: |
| (status, output) = ufw_allow_ip_list(new_ip, new_ip_list + old_ip_list, ssh_connect_args) |
| if status != "OK": |
| has_error = True |
| print("Error in %s. errmsg: %s" % (new_ip, output)) |
| if has_error is True: |
| #!/usr/bin/python |
Thursday, September 20, 2018
Benefits of Blockchain
Benefits of Blockchain
Monday, February 20, 2017
Application Centric Infrastructure with Python
| # Copyright (c) 2015 Cisco Systems # |
Sunday, October 2, 2016
Quick peek inside Kubernetes Containers.
Quick peek inside Kubernetes Containers
Sunday, November 30, 2014
Automate Mysql with Puppet
Automate Mysql with Puppet
To get started, I created a simple Client / Server based puppet scenario in which we will automate Mysql server implementation in a linux environment. Puppet master server is configured to provide modules, manifests and classes to the agent servers in the complete infrastructure.
After installing the puppet master server the users can use the following folder etc/puppet/manifests to develop and write some code for deploying the services to the agent servers.
First, on the puppet master server we should download and install the package module for the Mysql server using the following command:
sudo puppet module search mysql
After the successful installation you should verify that the folder mysql exists in the /etc/puppet/modules folder with the source installation of mysql server.
After the validation of folder existence , the source file are located there. Now we have to define the site.pp file inside the /etc/puppet/manifests as a manifest file that will send the code execution of installing the mysql server on an agent server. The content of the file should look like the following:
node 'agent_node01' { include mysql::server }
This line of code defines the agent_node01 will look for the installation of mysql server inside the mysql server module installation on the puppet master server. The next step is to connect to the agent node and start the puppet agent service.
sudo /etc/init.d/puppet start
After restarting the service I will initiate the agent test command to force the pulling the package and the module from the puppet master.
As we can see the catalog is finished and the Mysql server service is up and running. The allow_virtual parameter can be ignored as that is a deprecation warning by default.
Feel free to test and comment.
Saturday, September 27, 2014
Make linux process invisible with new Centos kernel
Make linux process invisible with new Centos kernel
Saturday, August 16, 2014
Using tcpdump with Linux
Using tcpdump with Linux
- -i any listen on any interface that is available on the system
- -n do not resolve hostnames
- -c get number of certain packets and then stop (usefull in not getting to much informations)
- -e get the Ethernet header along with the capture
- -E decrypt IPSEC traffic with providing a password key



















