{"id":748,"date":"2025-02-07T10:18:11","date_gmt":"2025-02-07T10:18:11","guid":{"rendered":"https:\/\/forumweb.hosting\/blog\/?p=748"},"modified":"2025-02-07T10:18:25","modified_gmt":"2025-02-07T10:18:25","slug":"how-to-monitor-server-performance-with-prometheus-and-grafana","status":"publish","type":"post","link":"https:\/\/forumweb.hosting\/blog\/how-to-monitor-server-performance-with-prometheus-and-grafana\/","title":{"rendered":"How to Monitor Server Performance with Prometheus and Grafana"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>Monitoring server performance is essential to ensure system reliability, identify bottlenecks, and maintain high availability. <strong>Prometheus<\/strong> and <strong>Grafana<\/strong> are powerful open-source tools that work seamlessly together to collect, visualize, and analyze server metrics in real-time. In this guide, you&#8217;ll learn how to set up Prometheus for data collection and Grafana for creating insightful dashboards to monitor your server\u2019s performance.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Before getting started, ensure you have:<\/p>\n<ul>\n<li>An Ubuntu 22.04 VPS<\/li>\n<li>Root or sudo privileges<\/li>\n<li>Basic knowledge of Linux commands<\/li>\n<\/ul>\n<h3>Step 1: Update Your System<\/h3>\n<p>Ensure your server is up-to-date to avoid compatibility issues:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo apt update &amp;&amp; sudo apt upgrade -y<\/p><\/blockquote>\n<h3>Step 2: Install Prometheus<\/h3>\n<p>Prometheus is a time-series database designed for monitoring and alerting. Follow these steps to install it:<\/p>\n<h4>1. Create a Prometheus User:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo useradd &#8211;no-create-home &#8211;shell \/bin\/false prometheus<\/p><\/blockquote>\n<h4>2. Create Directories for Prometheus Data:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo mkdir \/etc\/prometheus<br \/>\nsudo mkdir \/var\/lib\/prometheus<\/p><\/blockquote>\n<h4>3. Download Prometheus:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>cd \/tmp<br \/>\nwget https:\/\/github.com\/prometheus\/prometheus\/releases\/latest\/download\/prometheus-2.43.0.linux-amd64.tar.gz<\/p><\/blockquote>\n<h4>4. Extract the Archive:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>tar -xvf prometheus-2.43.0.linux-amd64.tar.gz<\/p><\/blockquote>\n<h4>5. Move Binaries:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo mv prometheus-2.43.0.linux-amd64\/prometheus \/usr\/local\/bin\/<br \/>\nsudo mv prometheus-2.43.0.linux-amd64\/promtool \/usr\/local\/bin\/<\/p><\/blockquote>\n<h4>6. Move Configuration Files:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo mv prometheus-2.43.0.linux-amd64\/consoles \/etc\/prometheus<br \/>\nsudo mv prometheus-2.43.0.linux-amd64\/console_libraries \/etc\/prometheus<br \/>\nsudo mv prometheus-2.43.0.linux-amd64\/prometheus.yml \/etc\/prometheus<\/p><\/blockquote>\n<h3>Step 3: Configure Prometheus<\/h3>\n<p>Edit the Prometheus configuration file:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo nano \/etc\/prometheus\/prometheus.yml<\/p><\/blockquote>\n<p>Example configuration:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>global:<br \/>\nscrape_interval: 15s<\/p>\n<p>scrape_configs:<br \/>\n&#8211; job_name: &#8216;prometheus&#8217;<br \/>\nstatic_configs:<br \/>\n&#8211; targets: [&#8216;localhost:9090&#8217;]<\/p><\/blockquote>\n<h3>Step 4: Set Up Prometheus as a Service<\/h3>\n<p>Create a systemd service file:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo nano \/etc\/systemd\/system\/prometheus.service<\/p><\/blockquote>\n<p>Add the following:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>[Unit]<br \/>\nDescription=Prometheus<br \/>\nWants=network-online.target<br \/>\nAfter=network-online.target<\/p>\n<p>[Service]<br \/>\nUser=prometheus<br \/>\nExecStart=\/usr\/local\/bin\/prometheus \\<br \/>\n&#8211;config.file=\/etc\/prometheus\/prometheus.yml \\<br \/>\n&#8211;storage.tsdb.path=\/var\/lib\/prometheus\/<\/p>\n<p>[Install]<br \/>\nWantedBy=multi-user.target<\/p><\/blockquote>\n<p>Reload systemd, start, and enable Prometheus:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo systemctl daemon-reload<br \/>\nsudo systemctl start prometheus<br \/>\nsudo systemctl enable prometheus<\/p><\/blockquote>\n<h4>Verify Prometheus:<\/h4>\n<p>Check if Prometheus is running:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo systemctl status prometheus<\/p><\/blockquote>\n<p>Access Prometheus at:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>http:\/\/your_server_ip:9090<\/p><\/blockquote>\n<h3>Step 5: Install Grafana<\/h3>\n<p>Grafana is used to visualize the data collected by Prometheus.<\/p>\n<h4>1. Add Grafana Repository:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo apt install -y software-properties-common<br \/>\nsudo add-apt-repository &#8220;deb https:\/\/packages.grafana.com\/oss\/deb stable main&#8221;<\/p><\/blockquote>\n<h4>2. Install Grafana:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo apt update &amp;&amp; sudo apt install grafana -y<\/p><\/blockquote>\n<h4>3. Start and Enable Grafana:<\/h4>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo systemctl start grafana-server<br \/>\nsudo systemctl enable grafana-server<\/p><\/blockquote>\n<h4>Access Grafana:<\/h4>\n<p>Visit Grafana at:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>http:\/\/your_server_ip:3000<\/p><\/blockquote>\n<p>Default login credentials:<\/p>\n<ul>\n<li>Username: <strong>admin<\/strong><\/li>\n<li>Password: <strong>admin<\/strong> (change on first login)<\/li>\n<\/ul>\n<h3>Step 6: Configure Prometheus as a Data Source in Grafana<\/h3>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Log into Grafana.<\/li>\n<li>Go to <strong>Settings \u2192 Data Sources<\/strong>.<\/li>\n<li>Click <strong>Add data source<\/strong> and select <strong>Prometheus<\/strong>.<\/li>\n<li>Set the URL to:<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<blockquote class=\"td_quote_box td_box_left\"><p>http:\/\/localhost:9090<\/p><\/blockquote>\n<ol>\n<li>Click <strong>Save &amp; Test<\/strong>.<\/li>\n<\/ol>\n<h3>Step 7: Create Dashboards in Grafana<\/h3>\n<p>To create a dashboard:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Click the <strong>+<\/strong> icon \u2192 <strong>Dashboard<\/strong>.<\/li>\n<li>Click <strong>Add new panel<\/strong>.<\/li>\n<li>In the query field, enter:<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<blockquote class=\"td_quote_box td_box_left\"><p>node_cpu_seconds_total<\/p><\/blockquote>\n<ol>\n<li>Adjust the visualization settings as needed.<\/li>\n<li>Click <strong>Apply<\/strong> to save the panel.<\/li>\n<\/ol>\n<h3>Step 8: Monitor Key Metrics<\/h3>\n<p>Common Prometheus metrics you can track:<\/p>\n<ul>\n<li><strong>CPU Usage:<\/strong><br \/>\n<blockquote class=\"td_quote_box td_box_left\"><p>rate(node_cpu_seconds_total[1m])<\/p><\/blockquote>\n<\/li>\n<li><strong>Memory Usage:<\/strong><br \/>\n<blockquote class=\"td_quote_box td_box_left\"><p>node_memory_MemAvailable_bytes<\/p><\/blockquote>\n<\/li>\n<li><strong>Disk Usage:<\/strong><br \/>\n<blockquote class=\"td_quote_box td_box_left\"><p>node_filesystem_avail_bytes<\/p><\/blockquote>\n<\/li>\n<li><strong>Network Traffic:<\/strong><br \/>\n<blockquote class=\"td_quote_box td_box_left\"><p>rate(node_network_receive_bytes_total[5m])<\/p><\/blockquote>\n<\/li>\n<\/ul>\n<h3>Step 9: Enable Alerts in Grafana<\/h3>\n<p>To set up alerts:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Go to your dashboard.<\/li>\n<li>Click the panel title \u2192 <strong>Edit<\/strong>.<\/li>\n<li>Go to the <strong>Alert<\/strong> tab and click <strong>Create Alert<\/strong>.<\/li>\n<li>Set conditions like:<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<blockquote class=\"td_quote_box td_box_left\"><p>WHEN avg() OF query(A, 5m, now) IS ABOVE 80<\/p><\/blockquote>\n<ol>\n<li>Save the alert configuration.<\/li>\n<\/ol>\n<h3>Step 10: Securing Prometheus and Grafana<\/h3>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Enable authentication for Prometheus by configuring <code>prometheus.yml<\/code>.<\/li>\n<li>Use HTTPS for Grafana by editing <code>\/etc\/grafana\/grafana.ini<\/code> and configuring SSL.<\/li>\n<li>Restrict access to monitoring tools using a firewall:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo ufw allow 9090\/tcp<br \/>\nsudo ufw allow 3000\/tcp<\/p><\/blockquote>\n<h3>Conclusion<\/h3>\n<p>Congratulations! You\u2019ve successfully set up Prometheus and Grafana to monitor your server&#8217;s performance. This powerful combination provides real-time insights into system metrics, helping you detect and respond to issues promptly. Regular monitoring ensures optimal performance, improves resource management, and enhances the security of your infrastructure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Monitoring server performance is essential to ensure system reliability, identify bottlenecks, and maintain high availability. Prometheus and Grafana are powerful open-source tools that work seamlessly together to collect, visualize, and analyze server metrics in real-time. In this guide, you&#8217;ll learn how to set up Prometheus for data collection and Grafana for creating insightful dashboards [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":749,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[147,145,41,146,75],"_links":{"self":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts\/748"}],"collection":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/comments?post=748"}],"version-history":[{"count":1,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts\/748\/revisions"}],"predecessor-version":[{"id":750,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts\/748\/revisions\/750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/media\/749"}],"wp:attachment":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/media?parent=748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/categories?post=748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/tags?post=748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}