jueves, 19 de diciembre de 2024

Migrate data from one collection to other

At the moment there is no command in MongoDB that would do this.
db.<collection_name>.find().forEach(function(d){ db.getSiblingDB('<new_database>')['<collection_name>'].insert(d); });

Quick and easy way to create a collection on mongodb

 

mongosh mongodb://admin:pass@localhost:27017
use sandbox
db.createCollection("synthetic");
db.synthetic.insertOne({ message: "Synthetic database initialized", timestamp: new Date() })
use admin
db.createUser({ user: "sandbox", pwd: "PassSand", roles: [ { role: "readWrite", db: "sandbox" } ] });

lunes, 16 de diciembre de 2024

Enable SNMP on Ubuntu Server

On the agent and manager server:

apt update
apt install snmp snmp-mibs-downloader
 
On the agent server:
vim /etc/snmp/snmp.conf  
/etc/snmp/snmp.conf

# As the snmp packages come without MIB files due to license reasons, loading # of MIBs is disabled by default. If you added the MIBs you can reenable # loading them by commenting out the following line.  

#mibs :

 
vim /etc/snmp/snmpd.conf
Comment the following lines: 
 #  Listen for connections from the local system only
#agentAddress  udp:127.0.0.1:161
#  Listen for connections on all interfaces (both IPv4 *and* IPv6)
agentAddress udp:161,udp6:[::1]:161
 
vim /etc/snmp/snmpd.conf
Add these lines: 
createUser bootstrap MD5 temp_password DES
rwuser bootstrap priv
rwuser demo priv 
 
systemctl restart snmpd
 
On the Manager server:
Verify with snmpget 
snmpget -u bootstrap -l authPriv -a MD5 -x DES -A temp_password -X temp_password agent_server_ip_address 1.3.6.1.2.1.1.1.0 
 
Output
SNMPv2-MIB::sysDescr.0 = STRING: Linux agent 4.15.0-66-generic #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019 x86_64
 
 
Create a user based on bootstrap 
snmpusm -u bootstrap -l authPriv -a MD5 -x DES -A temp_password -X temp_password agent_server_ip_address create demo bootstrap 
 Change the password
snmpusm -u demo -l authPriv -a MD5 -x DES -A temp_password -X temp_password agent_server_ip_address passwd temp_password new_password 

On the Agent server:
Remove the bootstrap
vim /etc/snmp/snmpd.conf 
Comment the following lines:
...
#createUser bootstrap MD5 temp_password DES
#rwuser bootstrap priv
...
 
On the Manager server:
snmpusm -u bootstrap -l authPriv -a MD5 -x DES -A temp_password -X temp_password 100.90.8.48 delete bootstrap
User successfully deleted.
 
On the Agent server:
systemctl restart snmpd
 

jueves, 12 de diciembre de 2024

How to Run Linux Commands in Background

 Using Ampersand
[command] &
 

Check the background processes' status in the current shell session with the jobs command jobs

Using System Redirects [command] output.log>&1 &
ping phoenixnap.com >/dev/null 2>&1 &

Using nohup
nohup myscript.sh >myscript.log 2>&1 </dev/null &

Bash script do a cleanup on exit

Excellent way to do a cleanup on exit.

#!/bin/bash

set -e # Exit on error

# Functions for cleanup on exit
cleanup() {
echo "Removing what is done..."
}
trap cleanup EXIT

lunes, 9 de diciembre de 2024

Install grafana

# sudo apt-get install -y apt-transport-https software-properties-common wget
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
wget is already the newest version (1.21.4-1ubuntu4.1).
wget set to manually installed.
The following NEW packages will be installed:
  apt-transport-https
The following packages will be upgraded:
  python3-software-properties software-properties-common
2 upgraded, 1 newly installed, 0 to remove and 54 not upgraded.
Need to get 48.1 kB of archives.
After this operation, 35.8 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu noble/universe amd64 apt-transport-https all 2.7.14build2 [3,974 B]
Get:2 http://us.archive.ubuntu.com/ubuntu noble-updates/main amd64 software-properties-common all 0.99.49.1 [14.4 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu noble-updates/main amd64 python3-software-properties all 0.99.49.1 [29.7 kB]
Fetched 48.1 kB in 0s (180 kB/s)                    
Selecting previously unselected package apt-transport-https.
(Reading database ... 83695 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_2.7.14build2_all.deb ...
Unpacking apt-transport-https (2.7.14build2) ...
Preparing to unpack .../software-properties-common_0.99.49.1_all.deb ...
Unpacking software-properties-common (0.99.49.1) over (0.99.48) ...
Preparing to unpack .../python3-software-properties_0.99.49.1_all.deb ...
Unpacking python3-software-properties (0.99.49.1) over (0.99.48) ...
Setting up apt-transport-https (2.7.14build2) ...
Setting up python3-software-properties (0.99.49.1) ...
Setting up software-properties-common (0.99.49.1) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for dbus (1.14.10-4ubuntu4.1) ...
Scanning processes...                                                                                                                                                                    
Scanning linux images...                                                                                                                                                                 

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host


 

# sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
root@pb-grafana-d001:~# echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main
root@pb-grafana-d001:~# echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com beta main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com beta main
root@pb-grafana-d001:~# # Updates the list of available packages
sudo apt-get update
Hit:1 http://us.archive.ubuntu.com/ubuntu noble InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu noble-updates InRelease                                                                                         
Get:3 https://apt.grafana.com stable InRelease [7,660 B]                                                                                                  
Hit:4 http://us.archive.ubuntu.com/ubuntu noble-backports InRelease                                   
Hit:5 http://security.ubuntu.com/ubuntu noble-security InRelease                          
Get:6 https://pkgs.tailscale.com/stable/ubuntu noble InRelease      
Get:7 https://apt.grafana.com beta InRelease [5,975 B]
Get:8 https://apt.grafana.com stable/main amd64 Packages [326 kB]
Get:9 https://apt.grafana.com beta/main amd64 Packages [1,616 B]
Fetched 348 kB in 1s (485 kB/s)
Reading package lists... Done
root@pb-grafana-d001:~# # Installs the latest OSS release:
sudo apt-get install grafana
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  musl
The following NEW packages will be installed:
  grafana musl
0 upgraded, 2 newly installed, 0 to remove and 54 not upgraded.
Need to get 127 MB of archives.
After this operation, 471 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu noble/universe amd64 musl amd64 1.2.4-2 [416 kB]
Get:2 https://apt.grafana.com stable/main amd64 grafana amd64 11.4.0 [127 MB]
Fetched 127 MB in 3s (45.8 MB/s)  
Selecting previously unselected package musl:amd64.
(Reading database ... 83699 files and directories currently installed.)
Preparing to unpack .../musl_1.2.4-2_amd64.deb ...
Unpacking musl:amd64 (1.2.4-2) ...
Selecting previously unselected package grafana.
Preparing to unpack .../grafana_11.4.0_amd64.deb ...
Unpacking grafana (11.4.0) ...
Setting up musl:amd64 (1.2.4-2) ...
Setting up grafana (11.4.0) ...
info: Selecting UID from range 100 to 999 ...

info: Adding system user `grafana' (UID 110) ...
info: Adding new user `grafana' (UID 110) with group `grafana' ...
info: Not creating home directory `/usr/share/grafana'.
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
 sudo /bin/systemctl daemon-reload
 sudo /bin/systemctl enable grafana-server
### You can start grafana-server by executing
 sudo /bin/systemctl start grafana-server
Processing triggers for man-db (2.12.0-4build2) ...
Scanning processes...                                                                                                                                                                    
Scanning linux images...                                                                                                                                                                 

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

 

 

# sudo systemctl daemon-reload
sudo systemctl start grafana-server


 

# sudo systemctl status grafana-server
● grafana-server.service - Grafana instance
     Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; preset: enabled)
     Active: active (running) since Mon 2024-12-09 21:12:22 UTC; 6s ago
       Docs: http://docs.grafana.org
   Main PID: 2728 (grafana)
      Tasks: 18 (limit: 962)
     Memory: 122.7M (peak: 143.1M swap: 1.7M swap peak: 1.7M)
        CPU: 1.368s
     CGroup: /system.slice/grafana-server.service
             └─2728 /usr/share/grafana/bin/grafana server --config=/etc/grafana/grafana.ini --pidfile=/run/grafana/grafana-server.pid --packaging=deb cfg:default.paths.logs=/var/log/gr>

Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.43369355Z level=info msg="Executing migration" id="create index UQE_dashboard_public_config_uid - v>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.434410666Z level=info msg="Migration successfully executed" id="create index UQE_dashboard_public_c>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.444885735Z level=info msg="Executing migration" id="create index IDX_dashboard_public_config_org_id>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.445705164Z level=info msg="Migration successfully executed" id="create index IDX_dashboard_public_c>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.456330875Z level=info msg="Executing migration" id="drop index UQE_dashboard_public_config_uid - v2"
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.457375016Z level=info msg="Migration successfully executed" id="drop index UQE_dashboard_public_con>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.469944357Z level=info msg="Executing migration" id="drop index IDX_dashboard_public_config_org_id_d>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.470633922Z level=info msg="Migration successfully executed" id="drop index IDX_dashboard_public_con>
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.489819777Z level=info msg="Executing migration" id="Drop public config table"
Dec 09 21:12:28 pb-grafana-d001 grafana[2728]: logger=migrator t=2024-12-09T21:12:28.490410095Z level=info msg="Migration successfully executed" id="Drop public config table" duration=>


 

# sudo systemctl enable grafana-server.service
Synchronizing state of grafana-server.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.

Install prometheus

Download prometheus

# wget https://github.com/prometheus/prometheus/releases/download/v3.0.1/prometheus-3.0.1.linux-amd64.tar.gz
--2024-12-09 22:12:17--  https://github.com/prometheus/prometheus/releases/download/v3.0.1/prometheus-3.0.1.linux-amd64.tar.gz
Resolving github.com (github.com)... 140.82.113.4
Connecting to github.com (github.com)|140.82.113.4|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/6838921/d29591b9-3d89-4c1b-85d3-0c4c38069329?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20241209%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241209T221217Z&X-Amz-Expires=300&X-Amz-Signature=6baf78ee0e923cc1f36128a8b4a85d8f03617a0804b49256d062adfbb0cd05fd&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dprometheus-3.0.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [following]
--2024-12-09 22:12:17--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/6838921/d29591b9-3d89-4c1b-85d3-0c4c38069329?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20241209%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241209T221217Z&X-Amz-Expires=300&X-Amz-Signature=6baf78ee0e923cc1f36128a8b4a85d8f03617a0804b49256d062adfbb0cd05fd&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dprometheus-3.0.1.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.109.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 112995464 (108M) [application/octet-stream]
Saving to: ‘prometheus-3.0.1.linux-amd64.tar.gz’


prometheus-3.0.1.linux-amd64.tar.gz            100%[=================================================================================================>] 107.76M  63.1MB/s    in 1.7s    

2024-12-09 22:12:19 (63.1 MB/s) - ‘prometheus-3.0.1.linux-amd64.tar.gz’ saved [112995464/112995464]

 

tar over the package

# tar xvfz prometheus-*.tar.gz
prometheus-3.0.1.linux-amd64/
prometheus-3.0.1.linux-amd64/promtool
prometheus-3.0.1.linux-amd64/LICENSE
prometheus-3.0.1.linux-amd64/prometheus
prometheus-3.0.1.linux-amd64/prometheus.yml
prometheus-3.0.1.linux-amd64/NOTICE


Move to the directory and start prometheus

# cd prometheus-3.0.1.linux-amd64/


# ./prometheus --config.file=./prometheus.yml &


Confirm that Prometheus is running by navigating to http://localhost:9090