cicorias
5/3/2017 - 11:01 PM

OMS Synthetic Data Generator

OMS Synthetic Data Generator

#!/usr/bin/env python

import sys
import logging

from time import sleep
import argparse

from web3 import Web3, IPCProvider


#for the API calls to OMS
import json
import requests
import datetime
import hashlib
import hmac
import base64

parser = argparse.ArgumentParser()
parser.parse_args()


logging.basicConfig(format='%(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

instance = { 'rpcHost': '', 'rpcPort':''}


def main():
    logger.info('just about to start: %s' 'true')

    for i in xrange(1, 200):
        try:
            logger.info('sending data..')
            send_data()
            logger.info('done')
            sleep(1)
        except Exception as e:
            logger.info(e)
            logger.info('Retry in 5secs')
            sleep(5)
            continue
        except:  # catch *all* exceptions
            e = sys.exc_info()[0]
            logger.info(e)
            logger.info('Rare exception')
            raise

    #https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api


def send_data():
    # Update the customer ID to your Operations Management Suite workspace ID
    customer_id = '???'

    # For the shared key, use either the primary or the secondary Connected Sources client authentication key   
    shared_key = '???'

    # The log type is the name of the event that is being submitted
    log_type = 'GethOmsAgentalpha003'

    from random import randint

    json_data = [{
        "EthNode":"foobar-tx1",
        "ID": "0cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": False,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": True
    },
    {
        "EthNode":"foobar-mn1",
        "ID": "1cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": True,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": True
    },
    {
        "EthNode":"foobar-tx2",
        "ID": "0cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": False,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": False
    },
    {
        "EthNode":"foobar-mn2",
        "ID": "1cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": True,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": False
    },
    {
        "EthNode":"foobar-tx3",
        "ID": "0cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": False,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": False
    },
    {
        "EthNode":"foobar-mn3",
        "ID": "1cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": True,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": False
    },
    {
        "EthNode":"foobar-tx4",
        "ID": "0cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": False,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": True
    },
    {
        "EthNode":"foobar-mn4",
        "ID": "1cdad72f-c848-4df0-8aaa-ffe033e75d52",
        "Version": "v1.5.6-unstable-e0ee0cc6",
        "NodeLatencyMs": randint(8,24),
        "Miner": True,
        "NumPeers": randint(18,25),
        "PendingTransactions": randint(35,37),
        "LastBlock": counter(),
        "LastBlockTimeMs": 17,
        "TotalDifficulty": 424242,
        "IsSyncing": True
    }]

    body = json.dumps(json_data)
   


    # json_data = [{
    # "slot_ID": 12345,
    #     "ID": "5cdad72f-c848-4df0-8aaa-ffe033e75d50",
    #     "availability_Value": 100,
    #     "performance_Value": 6.954,
    #     "measurement_Name": "last_one_hour",
    #     "duration": 3600,
    #     "warning_Threshold": 0,
    #     "critical_Threshold": 0,
    #     "IsActive": "true"
    # },
    # {   
    #     "slot_ID": 67890,
    #     "ID": "b6bee458-fb65-492e-996d-61c4d7fbb940",
    #     "availability_Value": 100,
    #     "performance_Value": 3.379,
    #     "measurement_Name": "last_one_hour",
    #     "duration": 3600,
    #     "warning_Threshold": 0,
    #     "critical_Threshold": 0,
    #     "IsActive": "false"
    # }]
    # body = json.dumps(json_data)

    post_data(customer_id, shared_key, body, log_type)


#####################
######Functions######  
#####################

# Build the API signature
def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource):
    x_headers = 'x-ms-date:' + date
    string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource
    bytes_to_hash = bytes(string_to_hash).encode('utf-8')  
    decoded_key = base64.b64decode(shared_key)
    encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest())
    authorization = "SharedKey {}:{}".format(customer_id,encoded_hash)
    return authorization

# Build and send a request to the POST API
def post_data(customer_id, shared_key, body, log_type):
    logger.info('Posting: %s', body)
    method = 'POST'
    content_type = 'application/json'
    resource = '/api/logs'
    rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
    content_length = len(body)
    signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource)
    uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01'


    headers = {
        'content-type': content_type,
        'Authorization': signature,
        'Log-Type': log_type,
        'x-ms-date': rfc1123date
    }

    #logger.info("Headers: %", json.dumps(headers))
    response = requests.post(uri,data=body, headers=headers)
    if (response.status_code >= 200 and response.status_code <= 299):
        logger.info('Accepted')
    else:
        logger.error('It failed')
        logger.error('Failed with %s', response.status_code)
        logger.error(response.content)
        logger.error(json.dumps(response.headers))
        #print "Response code: {}".format(response.status_code)

def counter():
    if 'cnt' not in counter.__dict__:
        counter.cnt = 0
    counter.cnt += 1
    return counter.cnt

if __name__ == '__main__':
    main()