How Do You Analyze Java Thread Dumps?

Problem scenario
You run Java programs on your Linux server. You want to view content of Java thread dumps. What should you do?

Solution
Prerequisites
Install Java if it has not already been installed. See this posting if you need assistance.

You may or may not want to learn more about what a Java thread is; you can read more here.

Procedures

  1. Find the PID of the Java program. To do this, run this:
sudo ps -ef | grep java

You should see this:

ubuntu   13687 13579  0 20:45 pts/3    00:00:00 java ContIntSleep
ubuntu   13702 13499  0 20:45 pts/2    00:00:00 grep --color=auto java

For the example above, 13687 is the PID you need. If you want a Java program that uses the sleep command (so it lasts for a while) use this program:

class ContIntSleep extends Thread{
 public void run(){
  for(int i=1;i<50;i++){
    try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
    System.out.println(i);
  }
 }
 public static void main(String args[]){
  ContIntSleep contint1=new ContIntSleep();
  ContIntSleep contint2=new ContIntSleep();

  contint1.start();
  contint2.start();
 }
}

Run this (but replace 13687 with the PID you found above):

jstack 13687

If you want to redirect the output to a file, you could run this:

jstack 13687 > /tmp/goodfile.txt

How Do You Troubleshoot the Cargo Problem “error: no such subcommand: add”?

Problem scenario
You run a Cargo command, but you get this:

error: no such command `add`
Did you mean `b
`?

What should you do?

Solution
Run this: sudo cargo install cargo-edit

It can take 15 minutes to run. This should eliminate the problem.

How Do You Get Python to Search a Web Page for a Specific Pattern?

Problem scenario
How do you write a Python 3 program to parse the text of a web page and determine if a string is on the web page or not?

Solution
Change the "yoururl" variable assignment and the "searchterm" assignment below to what you desire. Then run the program below:

import re
import requests
yoururl = 'https://www.continualintegration.com/'
searchterm = "learned"
r = requests.get(yoururl)
found = re.search(searchterm, r.text)
if found:
  print("The string/pattern '" + searchterm + "' was found when searching " + yoururl)
# found.group(0))
else:
  print("The string/pattern '" + searchterm + "' was not found when searching " + yoururl)

How Do You Write a Python Program to Test if Integers Are in Order?

Problem scenario
You need to write a Python program to see if a list of unique integers is in order. You can copy the list and sort it to compare the ideal state to the list provided. The program must print out a relevant message if a swap of two numbers can place those two numbers in the correct order. The program must print out if a given number is already in order.

Here are the requirements of the Python program:

  • generate a list of numbers (either by random or hard-code a list)
  • sort the list
  • find if incorrectly placed numbers share a spot with another number that if swapped, both would be in order

Solution

import random

def diff_finder(array):
  unsorted_list = array[:]
  array.sort()
  counter = 0
  for i in range(len(array)):
    if array[i] != unsorted_list[i]:
      if unsorted_list[array.index(unsorted_list[i])] == array[i]:   
      # if the integer in this position (the array.index(unsorted_list[i]) returns the position of the value of the wrong integer),
      # is the value of the correctly-ordered integer in the sorted array
        print("ONE SWAP WOULD CORRECTLY PLACE TWO NUMBERS!")
      else:
        print("It will take more than one swap with this number to get the list in order: " + str(unsorted_list[i]) )
    else:
      print(str(array[i]) + " is in correct order")

a_list = []

for i in range(10):
  a_list.append(random.randint(1,10))

print("This list we are dealing with is " + str(a_list))
diff_finder(a_list)

How Do You Configure Nginx to Listen to Messages Sent via HTTP?

Problem scenario
You have Nginx configured. The access logs never pick up any POST message content. You get 405 error messages. What do you do?

Solution
This will send a message payload -- but not via POST. It may not work for your situation. To get a message to be sent to a website (a listening web service), craft a URL that does not necessarily exist, but retain the domain name or IP address.

Run a command like this: curl http://x.x.x.x/SPECIALTESTTOAPPEAR
(But replace x.x.x.x with the domain name or the IP address of the website; replace the "SPECIALTESTTOAPPEAR" with the string of your choice.)

Go to the access.log file on the back-end of the Nginx server (e.g., run sudo find / -name access.log). You should see "SPECIALTESTTOAPPEAR" in the logs.

How Do You Solve the Kubernetes Error “networkPlugin cni failed to set up pod network: open /run/flannel/subnet.env: no such file or directory”?

Problem scenario
You run kubectl describe pods. You see a message in the output that says

networkPlugin cni failed to set up pod network: open /run/flannel/subnet.env: no such file or directory.

How do you fix this?

Possible Solution #1
Log into every worker node. Make sure it has /run/flannel/subnet.env with the following four lines:

FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

(This solution was adapted from https://www.programmersought.com/article/7824768991/)

Possible Solution #2
Run this: kubectl get pods. It could be that the error message shows up, but there is no problem. If the problem has since subsided, it may be ignorable.

Possible Solution #3
"More often than not, there is another issue that could be causing this error that needs to be investigated first."
(This one was taken from this Microsoft website.)

How Do You Troubleshoot “/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:83:in `require’: cannot load such file — winrm-elevated (LoadError)”?

Problem scenario
You run a Vagrant command. But you get this message: "/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- winrm-elevated (LoadError)"

What should you do?

Solution
Run this command:

vagrant plugin install winrm-elevated

What Is The Etymology of the Terms Level-Trigger and Edge-Trigger?

Problem scenario
You have read about level-trigger and edge-trigger notification mechanisms in Linux. Why are they called level-trigger and edge-trigger? Where do they get their names?

Answer
Electronics and processing electric signals. Edges represent changes to a state. A level state represents the lack of a change. Here is a diagram:

This answer was adapted from the following postings:
https://hackernoon.com/photos/QdWX77c6s5Skk99abuNLlG6AEZZ2-9w23y8s
http://venkateshabbarapu.blogspot.com/2013/03/edge-triggered-vs-level-triggered.html

FFR
Notification mechanisms are components of input and output in Linux. There are two main types of notification mechanisms in this context.

A level-triggered notification is comes from a file descriptor that "is considered to be ready if it is possible to perform an I/O system call without blocking." (Taken from page 1329 of The Linux Programming Interface.)

An edge-triggered notification is a notification that "is provided if there is I/O activity (e.g., new input) on a file descriptor since it was last monitored." (Taken from page 1329 of The Linux Programming Interface.)

Level-triggered notifications are used by the select and poll system calls (according to page 1329 of The Linux Programming Interface). LT notifications are also used by the epoll "I/O event notification facility" (as taken from the man page for epoll).

Edge-tiggered notifications are used by signal-drive I/O and the epoll "I/O event notification facility" (as taken from the man page for epoll) in Linux (according to page 1329 of The Linux Programming Interface).

How Do You Write a “Hello World” Command in Rust?

Problem scenario
You want to write a basic program in Rust to know you have it installed correctly. What do you do?

Solution

  1. Install Rust. If you need assistance, try this command:
sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2. Create a file called contint.rs with the following four lines:

// We recommend calling this contint.rs
fn main() {
    println!("Hello World!");
}
  1. Compile it with this command: rustc contint.rs
  2. Run the bytecode by using this command: ./contint

What Does the “-> str” Syntax Signify in Python Functions?

Problem scenario
You see Python code with this "-> str" syntax. What does it do?

Answer
It is purely for readability. To help your coworkers we recommend using the "-> str" syntax if a function returns a string. Otherwise the way to find out is to read the code or temporarily change the code and use type(variable_returned) to show the data type of that which is/was returned.

Here is a program that illustrates how we know the "-> str" is just for information and has no affect on the code:

def documentation_proof() -> str:
  var1 = "unmistakeably a string"
  #have either the above or below var1 definition uncommented. have only one.  it proves the "-> str" syntax is for information only.
  #var1 = 55
  return var1

check_output = documentation_proof()
print(check_output)


Some functions will be configured as "-> None" if the function returns nothing. (If the function actually does return something, the "-> None" will have no effect other than misleading people when they read the function itself.

Similarly, the parameters in a function's definition can display intended data types like this:

def nifty_function(a: int, b: str):

But those parameter data type definitions do nothing special beyond enhancing readability. If you passed a non-integer for "a" or a non-string for "b", the function would proceed as if there was no ": int" or ": str" in the function parameters. Some programmers do not use this explicit convention as it is arguably more prolix and makes the code busy to read.