Tuesday, November 15, 2022

Getting Peak Value and Closing for a Stock

This is helpful while filing IT returns.



from datetime import datetime
import yfinance as yf

for year in range(2016, 2022):
    start_date = datetime(year, 1, 1)
    end_date = datetime(year, 12, 31)
    data = yf.download('<Stock Ticker>', start=start_date, end=end_date)
    print('Year %s, Peak %s, Close %s' % (year, max(data.High), data.Close[-1]))

      

Getting the closing on a particular date


from datetime import datetime
import yfinance as yf

dates = ["2016-03-01",
"2016-06-01",
"2018-02-28",
"2018-08-31",
"2019-02-28",
"2017-07-31",
"2017-01-31",
"2017-01-31",
"2018-02-28",
"2016-07-31",
"2016-01-31",]
      
# Obtain all the data for X years.
start_date = datetime(2015, 1, 1)
end_date = datetime(2022, 11, 1)
# In the example below we're getting values for USD/INR
data = yf.download('INR=X', start=start_date, end=end_date)
# Reset Index so that Date can be queried.
df = data.reset_index()
for d in df.values:
    if str(d[0].date()) in dates:
        print(d[0], d[4])

Labels:

Tuesday, July 30, 2019

missing required virtualport type on RHEL

If you define vm with virtualinterfaces, that don't have virtualport, then cloning of such vms will fail with below error


*** libvirtError: XML error: missing required virtualport type



Eg xml for the vm, where you'd see the failure while cloning

38     type='bridge'>
39       address='52:54:00:8c:30:e1'/>
40       bridge='br0'/>
41       type='virtio'/>
42       
type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
43     
44     type='bridge'>
45       address='52:54:00:e6:70:f8'/>
46       bridge='nsx-managed'/>
47       type='openvswitch'>
48         interfaceid='df55d7eb-5f25-43db-8bf8-f67b259bf51b'/>
49       
50       type='virtio'/>
51       
type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
52     

A bad work-around is to comment out the line which does this validation


/usr/share/virt-manager/virtinst/cloner.py


     def start_duplicate(self, meter=None):
         """
         Actually perform the duplication: cloning disks if needed and defining
         the new clone xml.
         """
         logging.debug("Starting duplicate.")
         meter = util.ensure_meter(meter)

         dom = None
         try:
             # Replace orig VM if required
             Guest.check_vm_collision(self.conn, self.clone_name,
                                      do_remove=self.replace)

             # Define domain early to catch any xml errors before duping storage
             # The below statement was commented out for systest. While cloning a VM
             # with no virtual port defined, it will error out
             #dom = self.conn.defineXML(self.clone_xml)
             dom = None
             if self.preserve:
                 for dst_dev in self.clone_disks:
                     dst_dev.setup(meter=meter)
                 if self._nvram_disk:
                     self._nvram_disk.setup(meter=meter)
         except Exception as e:
             logging.debug("Duplicate failed: %s", str(e))
             if dom:
                 dom.undefine()

             raise


Plotting performance results of kubetest

kubetest is a open source test suite to test various aspects of Kubernetes cluster. One of the areas that of interest for a scaled environment is the Performance of cluster.

Configuring/Running kubetest is beyond scope of this blog - what I intend to share is how you can plot the results of these tests

API Responsiveness:



import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import json

data = json.load(open('PM1_APIResponsiveness_load_2019-06-19T15:11:48Z.json'))['dataItems']
perc99_data = []
for item in data:
  myl = []
  myl.append(item['labels']['Verb'] + "-" +item['labels']['Scope'] + '-' + item['labels']['Resource'])
  myl.append(item['data']['Perc50'])
  myl.append(item['data']['Perc90'])
  myl.append(item['data']['Perc99'])
  perc99_data.append(myl)
df = pd.DataFrame(perc99_data, columns = ['API', 'LatencyP50', 'LatencyP90', 'LatencyP99'])
df.plot.bar()
index = np.arange(len(perc99_data))
plt.bar(index, df['LatencyP50'])
plt.xlabel("APIs")
plt.ylabel("Latency (ms)")
plt.xticks(index, df['API'], fontsize=5, rotation=30, ha='right')
plt.show()



cradhakrish-a01:chirag cradhakrishnan$ cat PM1_APIResponsiveness_load_2019-06-19T15\:11\:48Z.json 
{
  "version": "v1",
  "dataItems": [
    {
      "data": {
        "Perc50": 5.484,
        "Perc90": 25.443,
        "Perc99": 114.011
      },
      "unit": "ms",
      "labels": {
        "Count": "2358",
        "Resource": "pods",
        "Scope": "namespace",
        "Subresource": "",
        "Verb": "DELETE"
      }
    },
    {
      "data": {
        "Perc50": 32.052,
        "Perc90": 51.073,
        "Perc99": 74.26
      },
      "unit": "ms",
      "labels": {
        "Count": "68",
        "Resource": "services",
        "Scope": "namespace",
        "Subresource": "",
        "Verb": "DELETE"
      }
.
.
.

    {
      "data": {
        "Perc50": 0.115,
        "Perc90": 0.178,
        "Perc99": 0.273
      },
      "unit": "ms",
      "labels": {
        "Count": "38",
        "Resource": "secrets",
        "Scope": "namespace",
        "Subresource": "",
        "Verb": "LIST"
      }
    }
  ]
}



Pod Responsiveness


import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import json

data = json.load(open('PM1_PodStartupLatency_density_2019-06-20T10:32:57Z.json'))['dataItems']
perc99_data = []
for item in data:
  myl = []
  myl.append(item['labels']['Metric'])
  myl.append(item['data']['Perc50'])
  myl.append(item['data']['Perc90'])
  myl.append(item['data']['Perc99'])
  myl.append(item['data']['Perc100'])
  perc99_data.append(myl)
df = pd.DataFrame(perc99_data, columns = ['Metric', 'LatencyP50', 'LatencyP90', 'LatencyP99', 'LatencyP100'])
df.plot.bar()
index = np.arange(len(perc99_data))
plt.bar(index, df['LatencyP50'])
plt.xlabel("Metric")
plt.ylabel("Latency (ms)")
plt.xticks(index, df['Metric'], fontsize=5, rotation=30, ha='right')
plt.show()

cradhakrish-a01:chirag cradhakrishnan$ cat PM1_PodStartupLatency_density_2019-06-20T10:32:57Z.json
{
  "version": "v1",
  "dataItems": [
    {
      "data": {
        "Perc100": 0,
        "Perc50": 0,
        "Perc90": 0,
        "Perc99": 0
      },
      "unit": "ms",
      "labels": {
        "Metric": "create_to_schedule"
      }
    },
    {
      "data": {
        "Perc100": 28000,
        "Perc50": 9000,
        "Perc90": 14000,
        "Perc99": 26000
      },
      "unit": "ms",
      "labels": {
        "Metric": "schedule_to_run"
      }
    },
    {
      "data": {
        "Perc100": -5327.442072,
        "Perc50": -6894.455178,
        "Perc90": -6155.301798,
        "Perc99": -5537.035922
      },
      "unit": "ms",
      "labels": {
        "Metric": "run_to_watch"
      }
    },
    {
      "data": {
        "Perc100": 20665.27986,
        "Perc50": 1753.811386,
        "Perc90": 6665.07306,
        "Perc99": 18600.530946
      },
      "unit": "ms",
      "labels": {
        "Metric": "schedule_to_watch"
      }
    },
    {
      "data": {
        "Perc100": 20665.27986,
        "Perc50": 1753.811386,
        "Perc90": 6665.07306,
        "Perc99": 18600.530946
      },
      "unit": "ms",
      "labels": {
        "Metric": "pod_startup"
      }
    }
  ]
}

Friday, September 22, 2017

How to remove vnic from DVS on ESX without VC!

While moving from normal port group to a DVS, we faced an issue with vCenter. The vCenter lost its IP address and we had no option but to remove the vnic that was added to a DVS, back to normal port group.
But how to do you do this without vCenter?!

Thankfully, there are some command lines available.

Here are the steps:

1. Identify the vnmic#, Dswitch name and dvport id using following command

[root@w2-nsbu-systest-esx050:~] esxcli network vswitch dvs vmware list
DSwitch_VM_Network
   Name: DSwitch_VM_Network
   VDS ID: 50 3a a0 2c 2c 55 5e c0-77 20 4d 17 f9 b5 ee 10
   Class: etherswitch
   Num Ports: 8192
   Used Ports: 4
   Configured Ports: 512
   MTU: 1500
   CDP Status: listen
   Beacon Timeout: -1
   Uplinks: vmnic3
   VMware Branded: true
   DVPort:
         Client: vmnic3
         DVPortgroup ID: dvportgroup-60
         In Use: true
         Port ID: 59

         Client:
         DVPortgroup ID: dvportgroup-60
         In Use: false

         Port ID: 60

2. Remove the vnic using following command:

[root@w2-nsbu-systest-esx050:~] esxcfg-vswitch -Q vmnic3 -V 59 DSwitch_VM_Network

Voila! vmnic3 in this case is disconnected from DSwitch_VM_Network. You can now associate this with a normal port group.



Monday, April 10, 2017

Unable to access virsh console of a KVM VM.

Often when I download qcow2 images from internet and access them using virsh console , I can't see the console.
Here's what we need to do:

Note: /vms/images/ubuntu_1404_v05.qcow2 is the qcow2 image for which we're unable to access the console.

sudo apt-get install qemu-utils
sudo modprobe nbd max_part=8
sudo qemu-nbd --connect=/dev/nbd0 /vms/images/ubuntu_1404_v05.qcow2
sudo fdisk /dev/nbd0 -l
sudo mount /dev/nbd0p1 /mnt

for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done

chroot /mnt

vi /etc/default/grub and modify the parameters
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"
GRUB_CMDLINE_LINUX="console=ttyS0"

update-grub


sudo umount /mnt

sudo qemu-nbd --disconnect /dev/nbd0

Wednesday, February 15, 2017

Creating unique interfaceid in KVM (RHEL)

Consider the KVM VM has the below definition:



If you clone this VM using virt-clone utility, then the new VM will have the same interface id


If you need new interfaceid for every clone, then some libvirt py files will need change:

1) /usr/share/virt-manager/virtinst/deviceinterface.py

class VirtualPort(XMLBuilder):
    _XML_ROOT_NAME = "virtualport"

    type = XMLProperty("./@type")
    managerid = XMLProperty("./parameters/@managerid", is_int
    typeid = XMLProperty("./parameters/@typeid", is_int=True)
    typeidversion = XMLProperty("./parameters/@typeidversion"
    instanceid = XMLProperty("./parameters/@instanceid")
    interfaceid = XMLProperty("./parameters/@interfaceid")   <--- added="" div="" newly="">

2) /usr/share/virt-manager/virtinst/cloner.py

import uuid

and following line:

for iface in self._guest.get_devices("interface"):
    iface.target_dev = None
    if clone_macs:
        mac = clone_macs.pop()
    else:
        mac = VirtualNetworkInterface.generate_mac(self.conn)
    iface.macaddr = mac
    if iface.virtualport.type:
        iface.virtualport.interfaceid = uuid.uuid4() <--- added="" div="" newly="">




Monday, April 4, 2016

Adding variables in globals() of pdb

Don't know if this is a bug with python pdb, but if you try accessing variables inside a lambda function, it doesn't work in pdb session.
This makes it difficult to debug your code which has lambda

(Pdb++) ls = nat_operations.get_ls_connected_to_tier(topology, lambda x: x == lr)

*** NameError: global name 'lr' is not defined

The variable is otherwise accessible:
(Pdb++) lr

Add the variable to global:

(Pdb++) globals()['lr'] = lr

Then the function works correctly:

(Pdb++) ls = nat_operations.get_ls_connected_to_tier(topology, lambda x: x == lr)
(Pdb++) ls

Labels: , ,