Skip to content

Commit

Permalink
Merge pull request #4 from smortex/gem
Browse files Browse the repository at this point in the history
Modernize the project root
  • Loading branch information
jamtur01 authored Jul 7, 2024
2 parents bcc96cb + 021fbc9 commit f041c9e
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 79 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "bundler" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '2.7'
bundler-cache: true
- name: Run rubocop
run: bundle exec rubocop
#test:
# needs: lint
# runs-on: ubuntu-latest
# strategy:
# matrix:
# ruby-version:
# - 2.6
# - 2.7
# - 3.0
# - 3.1
# steps:
# - uses: actions/checkout@v3
# - name: Setup Ruby
# uses: ruby/setup-ruby@v1
# with:
# ruby-version: ${{ matrix.ruby-version }}
# bundler-cache: true
# - name: Run the test suite
# run: bundle exec rspec
72 changes: 72 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]
schedule:
- cron: '27 16 * * 6'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'ruby' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Gemfile.lock
pkg/
._*
*~
Expand Down
43 changes: 43 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
require:
- rubocop-rake
- rubocop-rspec
AllCops:
Exclude:
- lib/riemann/tools/*_parser.tab.rb
- vendor/bundle/**/*
Gemspec/RequiredRubyVersion:
Enabled: false
Layout/HashAlignment:
EnforcedHashRocketStyle: table
Layout/LineLength:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/MethodLength:
Enabled: false
Metrics/ParameterLists:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Naming/MethodParameterName:
AllowedNames:
- az # availability zone
- id
- lb # load balancer
Style/Documentation:
Enabled: false
Style/HashSyntax:
EnforcedShorthandSyntax: never
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in riemann-postgresql.gemspec
gemspec
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'riemann/tools/postgresql/version'
require 'bundler/gem_tasks'
require 'github_changelog_generator/task'

GitHubChangelogGenerator::RakeTask.new :changelog do |config|
config.user = 'riemann'
config.project = 'riemann-postgresql'
config.exclude_labels = ['skip-changelog']
config.future_release = "v#{Riemann::Tools::Postgresql::VERSION}"
end
40 changes: 0 additions & 40 deletions Rakefile.rb

This file was deleted.

73 changes: 34 additions & 39 deletions lib/riemann/tools/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,66 @@ class Postgresql
include Riemann::Tools
require 'pg'

opt :postgresql_host, "PostgreSQL Server Hostname", :type => String, :default => "localhost"
opt :postgresql_port, "PostgreSQL Server Port", :default => 5432
opt :postgresql_username, "Authenticated username", :type => String, :default => "postgres"
opt :postgresql_password, "User's password", :type => String, :default => "postgres"
opt :postgresql_database, "Database to connect", :type => String, :default => "postgres"
opt :postgresql_host, 'PostgreSQL Server Hostname', type: String, default: 'localhost'
opt :postgresql_port, 'PostgreSQL Server Port', default: 5432
opt :postgresql_username, 'Authenticated username', type: String, default: 'postgres'
opt :postgresql_password, "User's password", type: String, default: 'postgres'
opt :postgresql_database, 'Database to connect', type: String, default: 'postgres'

def initialize
begin
@conn = PG.connect(:host => opts[:postgresql_host],
:port => opts[:postgresql_port],
:user => opts[:postgresql_username],
:password => opts[:postgresql_password],
:dbname => opts[:postgresql_database])
rescue
PG::Error
puts "Error: Unable to connect with PostgreSQL server."
exit 1
end
@conn = PG.connect(host: opts[:postgresql_host],
port: opts[:postgresql_port],
user: opts[:postgresql_username],
password: opts[:postgresql_password],
dbname: opts[:postgresql_database],)
rescue PG::Error
puts 'Error: Unable to connect with PostgreSQL server.'
exit 1
end

def tick
@conn.transaction do

# General DB statistics.
@conn.exec("DECLARE general CURSOR FOR SELECT pg_stat_database.*, pg_database_size \
(pg_database.datname) AS size FROM pg_database JOIN pg_stat_database ON \
pg_database.datname = pg_stat_database.datname WHERE pg_stat_database.datname \
pg_database.datname = pg_stat_database.datname WHERE pg_stat_database.datname \
NOT IN ('template0', 'template1', 'postgres')")

result = @conn.exec("FETCH ALL IN general")
result = @conn.exec('FETCH ALL IN general')

keys = result.fields.collect.to_a
result.values.collect do |row|
vals = row.collect.to_a
vals.each_with_index {|val, index|
if index > 1
report(
:host => opts[:postgresql_host].dup,
:service => "DB #{vals[1]} #{keys[index]}",
:metric => vals[index].to_f,
:state => 'ok',
:description => "PostgreSQL DB #{keys[index]}".gsub("_", " "),
:tags => ['postgresql']
)
end
}
vals.each_with_index do |_val, index|
next unless index > 1

report(
host: opts[:postgresql_host].dup,
service: "DB #{vals[1]} #{keys[index]}",
metric: vals[index].to_f,
state: 'ok',
description: "PostgreSQL DB #{keys[index]}".gsub('_', ' '),
tags: ['postgresql'],
)
end
end

# Each DB specific connection counts.
@conn.exec("DECLARE connection CURSOR FOR SELECT datname, count(datname) FROM pg_stat_activity \
GROUP BY pg_stat_activity.datname")

result = @conn.exec("FETCH ALL in connection")
result = @conn.exec('FETCH ALL in connection')
result.values.collect do |row|
vals = row.collect.to_a
report(
:host => opts[:postgresql_host].dup,
:service => "DB #{vals[0]} connections",
:metric => vals[1].to_f,
:state => 'ok',
:description => "PostgreSQL DB Connections",
:tags => ['postgresql']
host: opts[:postgresql_host].dup,
service: "DB #{vals[0]} connections",
metric: vals[1].to_f,
state: 'ok',
description: 'PostgreSQL DB Connections',
tags: ['postgresql'],
)
end

end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/riemann/tools/postgresql/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Riemann
module Tools
class Postgresql
VERSION = '0.1.1'
end
end
end
40 changes: 40 additions & 0 deletions riemann-postgresql.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require_relative 'lib/riemann/tools/postgresql/version'

Gem::Specification.new do |spec|
spec.name = 'riemann-postgresql'
spec.version = Riemann::Tools::Postgresql::VERSION
spec.authors = ['Pradeep Chhetri']
spec.email = ['[email protected]']

spec.summary = 'PostgreSQL Riemann Client'
spec.homepage = 'https://github.com/riemann/riemann-postgresql'
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')

spec.metadata['allowed_push_host'] = 'https://rubygems.org/'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = spec.homepage

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'pg', '>= 0.17.1'
spec.add_runtime_dependency 'riemann-tools', '>= 0.2.1'

spec.add_development_dependency 'github_changelog_generator'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rubocop-rake'
spec.add_development_dependency 'rubocop-rspec'
end

0 comments on commit f041c9e

Please sign in to comment.