Hoster.by

Dashboard

Сбор метрик личного кабинета hoster.by
Last updated: 6 months ago

Start with Grafana Cloud and the new FREE tier. Includes 10K series Prometheus or Graphite Metrics and 50gb Loki Logs

Downloads: 3

Reviews: 0

  • Hoster.by.png
    Hoster.by.png

Telegraf config:

[global_tags]

[agent]
  interval = "10s"
  round_interval = true

  metric_buffer_limit = 1000
  flush_buffer_when_full = true

  collection_jitter = "0s"

  flush_interval = "10s"
  flush_jitter = "0s"

  debug = false
  quiet = false
  logfile = "/Program Files/Telegraf/telegraf.log"

  hostname = ""

[[outputs.influxdb]]
  urls = ["http://influxdb:8086"] 
  database = "database"
  precision = "s"

  timeout = "5s"
  username = "username"
  password = "password"

[[inputs.exec]]
  interval = "1h"
  commands = ["powershell Get-HosterBYMetrics -email 'email' -password 'password'"]
  timeout = "1m"
  name_suffix = ""
  data_format = "influx"

Collector Configuration Details

Windows\System32\WindowsPowerShell\v1.0\Modules\Influxdata.Telegraf.HosterBY\Influxdata.Telegraf.HosterBY.psm1

[console]::OutputEncoding = [System.Text.Encoding]::UTF8
$uri = 'https://clientapi.hoster.by'

function isEmpty($value) {
    if ($value -is [Bool]) { return $value } # True or False
    if (!$value) { return "0" } # Empty string or Null
    else {
        if ($value -is [String]) { return $value.Replace(" ", "\ ").Replace(",", ".") }
        else { return $value } # String or Integer
    }
}

function Get-HosterBYToken {
    param (
        [parameter(Mandatory = $true)][string]$email,
        [parameter(Mandatory = $true)][string]$password,
        [bool]$remote = $false
    )
    
    try {
        $response = Invoke-RestMethod -Method 'Post' -Uri ($uri + '/user/authorization') -Body @{email = $email; password = $password; remote = $false}
    }
    catch {
        #$_.Exception.Response.StatusCode.value__ #status code 200, 404, etc.
        #$_.Exception.Response.StatusDescription #status description
        return $null
    }
    return $response.tokens.jwt_access
}

function Get-HosterBYRequestResponse {
    param (
        [parameter(Mandatory = $true)][string]$request,
        [parameter(Mandatory = $true)][string]$method,
        [parameter(Mandatory = $true)][string]$token
    )
    
    try {
        $response = Invoke-RestMethod -Method $method -Uri ($uri + $request) -Headers @{Authorization = "Bearer " + $token}
    }
    catch {
        return $null
    }
    return $response
}

function Get-HosterBYMetrics {
    param (
        [parameter(Mandatory = $true)][string]$email,
        [parameter(Mandatory = $true)][string]$password
    )
    
    $token = Get-HosterBYToken -email $email -password $password
    if ($token -eq $null) { return }
    
    $domain_orders = Get-HosterBYRequestResponse -request '/domain/orders?limit=10000' -method 'Get' -token $token
    if ($domain_orders -ne $null) {
        $domain_orders.orders | % {
            if ($_.order_status) { $order_status = 1 } else { $order_status = 0 }
            $base   = "hosterby_domain"
            $tags   = "id={0},domain={1},expiration_date={2},creation_date={3},has_recurrent={4}"
            $values = "order_status={5}i,expiration_days={6}i"
            $output = $base + "," + $tags + " " + $values
            $output -f (isEmpty($_.id)), (isEmpty($_.domain)), (isEmpty($_.expiration_date.Substring(0, 10))), (isEmpty($_.creation_date.Substring(0, 10))), (isEmpty($_.has_recurrent)), `
                $order_status, (([Datetime]::ParseExact($_.expiration_date.substring(0, 19), 'yyyy-MM-ddTHH:mm:ss', $null)).AddHours($_.expiration_date.substring(20, 2)) - (Get-Date)).Days
        }
    }
    
    $hosting_orders = Get-HosterBYRequestResponse -request '/hosting/orders?limit=10000' -method 'Get' -token $token
    if ($hosting_orders -ne $null) {
        $hosting_orders.orders | % {
            $base   = "hosterby_hosting"
            $tags   = "id={0},domain={1},tariff_name={2},expiration_date={3},disconnection_date={4},can_promise={5},has_recurrent={6},order_status={7}"
            $values = "expiration_days={8}i"
            $output = $base + "," + $tags + " " + $values
            $output -f (isEmpty($_.id)),  (isEmpty($_.domain)), (isEmpty($_.tariff_name)), (isEmpty($_.expiration_date.Substring(0, 10))), (isEmpty($_.disconnection_date.Substring(0, 10))), (isEmpty($_.can_promise)), `
                (isEmpty($_.has_recurrent)), (isEmpty($_.order_status)), `
                (([Datetime]::ParseExact($_.expiration_date.substring(0, 19), 'yyyy-MM-ddTHH:mm:ss', $null)).AddHours($_.expiration_date.substring(20, 2)) - (Get-Date)).Days
        }
    }
    
    $email_orders = Get-HosterBYRequestResponse -request '/emails/orders?limit=10000' -method 'Get' -token $token
    if ($email_orders -ne $null) {
        $email_orders.orders | % {
            if ($_.disk_quota -and $_.disk_used) { $disk_used_percentage = ($_.disk_used / $_.disk_quota) * 100 } else { $disk_used_percentage = 0 }
            $base   = "hosterby_email"
            $tags   = "id={0},user_id={1},tariff_id={2},expiration_date={3},tariff_name={4},parent_service_id={5},parent_service_type={6},bezlimit={7},is_parent_service={8},service_type={9}i,mail_domains={10},order_status={11}"
            $values = "disable_auto_invoice={12}i,mute_notification={13}i,monthly_invoice={14}i,domains_quota={15}i,accounts_quota={16}i,disk_quota={17},disk_used={18},accounts_used={19}i,domains_used={20}i,expiration_days={21}i,disk_used_percentage={22}i"
            $output = $base + "," + $tags + " " + $values
            $output -f (isEmpty($_.id)), (isEmpty($_.user_id)), (isEmpty($_.tariff_id)), (isEmpty($_.expiration_date.Substring(0, 10))), (isEmpty($_.tariff_name)), (isEmpty($_.parent_service_id)), `
                (isEmpty($_.parent_service_type)), (isEmpty($_.bezlimit)), (isEmpty($_.is_parent_service)), (isEmpty($_.service_type)), (isEmpty($_.mail_domains)), (isEmpty($_.order_status)), `
                (isEmpty($_.disable_auto_invoice)), (isEmpty($_.mute_notification)), (isEmpty($_.monthly_invoice)), (isEmpty($_.domains_quota)), (isEmpty($_.accounts_quota)), (isEmpty($_.disk_quota)), `
                (isEmpty($_.disk_used)), (isEmpty($_.accounts_used)), (isEmpty($_.domains_used)), `
                (([Datetime]::ParseExact($_.expiration_date.substring(0, 19), 'yyyy-MM-ddTHH:mm:ss', $null)).AddHours($_.expiration_date.substring(20, 2)) - (Get-Date)).Days, `
                [math]::Round($disk_used_percentage, 0)
            
            $mail_domains = Get-HosterBYRequestResponse -request ('/emails/orders/' + $_.id + '/mail-domains?limit=100') -method 'Get' -token $token
            if ($mail_domains -ne $null) {
                $mail_domains.mail_domains | % {
                                        
                    Get-HosterBYRequestResponse -request ('/emails/orders/' + $_.email_order_id + '/mail-domains/' + $_.id) -method 'Put' -token $token > null
                    $mail_accounts = Get-HosterBYRequestResponse -request ('/emails/orders/' + $_.email_order_id + '/mail-domains/' + $_.id + '/mail-accounts?limit=50000') -method 'Get' -token $token
                    if ($mail_accounts -ne $null) {
                        $domain = $_.domain
                        $email_order_id = $_.email_order_id
                        $mail_domain_id = $_.id
                        $mail_accounts.mail_accounts | % {
                            if ($_.used_disk -and $_.quota_disk) { $used_disk_percentage = ($_.used_disk / $_.quota_disk) * 100 } else { $used_disk_percentage = 0 }
                            $base   = "hosterby_mail_accounts"
                            $tags   = "id={0},name={1},aliases={2},domain_id={3},delete={4},domain={5},email_order_id={6},mail_domain_id={7}"
                            $values = "used_disk={8},quota_disk={9},used_disk_percentage={10}i"
                            $output = $base + "," + $tags + " " + $values
                            $output -f (isEmpty($_.id)), (isEmpty($_.name)), (isEmpty($_.aliases)), (isEmpty($_.domain_id)), (isEmpty($_.delete)), (isEmpty($domain)), (isEmpty($email_order_id)), `
                            (isEmpty($mail_domain_id)), (isEmpty($_.used_disk)), (isEmpty($_.quota_disk)), [math]::Round($used_disk_percentage, 0)
                        }
                    }
                }
            }
        }
    }
}
Dependencies: